[英]JavaFx Transparent Stage and still Paint
I am trying to make a window that is transparent but I can still draw on and the drawings are not transparent. 我正在尝试制作一个透明的窗口,但是我仍然可以在其上绘制并且图纸也不透明。 If i put opacity on the stage it makes everything transparent. 如果我在舞台上放置不透明度,则可使所有内容透明。 I also have tried adding a transparency to the group but that doesn't seem to change anything. 我也尝试过为小组增加透明度,但这似乎并没有改变任何事情。 How can i accomplish this? 我怎样才能做到这一点?
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();
So, based on how to make transparent scene and stage in javafx? 那么,基于如何在javafx中制作透明的场景和舞台呢? , I updated you code, adding ,我更新了您的代码,添加了
secondScene.setFill(Color.TRANSPARENT);
and 和
newWindow.initStyle(StageStyle.TRANSPARENT);
And was able to produce... 并能够生产...
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);
}
}
Remember, when dealing with these kind of things, you need to make sure that every layer in the hierarchy is also transparent 请记住,在处理这类事情时,您需要确保层次结构中的每一层也是透明的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.