我正在尝试制作一个透明的窗口,但是我仍然可以在其上进行绘制,而图纸也不是透明的。如果我在舞台上放置不透明度,则可使所有内容透明。我也尝试过为小组增加透明度,但这似乎并没有改变任何事情。我该怎么做?
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode displayMode = gd.getDisplayMode();
Canvas canvas = new Canvas(displayMode.getWidth(), displayMode.getHeight());
GraphicsContext gc = canvas.getGraphicsContext2D();
Group group = new Group();
group.getChildren().add(canvas);
Scene secondScene = new Scene(group, displayMode.getWidth(), displayMode.getHeight());
gc.setFill(Color.rgb(0, 0, 0, .5));
gc.fillRect(0, 0, 100, 100);
// New window (Stage)
Stage newWindow = new Stage(StageStyle.UNDECORATED);
newWindow.setOpacity(.02);
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
// Set position of second window, related to primary window.
newWindow.setX(0);
newWindow.setY(0);
//newWindow.setWidth(displayMode.getWidth());
//newWindow.setHeight(displayMode.getHeight());
newWindow.show();
答案 0 :(得分:2)
因此,根据how to make transparent scene and stage in javafx?,我更新了您的代码,添加了
secondScene.setFill(Color.TRANSPARENT);
和
newWindow.initStyle(StageStyle.TRANSPARENT);
并且能够生产...
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
public class Test extends Application {
@Override
public void start(Stage primaryStage) {
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
DisplayMode displayMode = gd.getDisplayMode();
Canvas canvas = new Canvas(displayMode.getWidth(), displayMode.getHeight());
GraphicsContext gc = canvas.getGraphicsContext2D();
Group group = new Group();
group.getChildren().add(canvas);
Scene secondScene = new Scene(group, displayMode.getWidth(), displayMode.getHeight());
secondScene.setFill(Color.TRANSPARENT); gc.setFill(Color.rgb(0, 0, 0, .5));
gc.fillRect(0, 0, 100, 100);
// New window (Stage)
Stage newWindow = new Stage(StageStyle.UNDECORATED);
newWindow.initStyle(StageStyle.TRANSPARENT);
newWindow.setTitle("Second Stage");
newWindow.setScene(secondScene);
// Set position of second window, related to primary window.
newWindow.setX(0);
newWindow.setY(0);
// newWindow.setWidth(200);
// newWindow.setHeight(200);
newWindow.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}
请记住,在处理这类事情时,您需要确保层次结构中的每一层也是透明的