【问题标题】:JavaFx Transparent Stage and still PaintJavaFx 透明舞台和仍然绘画
【发布时间】:2018-12-15 13:59:39
【问题描述】:

我正在尝试制作一个透明的窗口,但我仍然可以在上面绘图并且图纸不透明。如果我把不透明放在舞台上,它会让一切变得透明。我也尝试过为组添加透明度,但这似乎并没有改变任何东西。我怎样才能做到这一点?

        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();

【问题讨论】:

标签: java javafx


【解决方案1】:

所以,基于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);
    }

}

记住,在处理这类事情时,你需要确保层次结构中的每一层也是透明的

【讨论】:

  • 哇,我实际上尝试了其中一个,但我想我从未尝试在场景和舞台上添加透明。我想这是有道理的,哈哈。非常感谢!!
猜你喜欢
  • 2017-12-17
  • 1970-01-01
  • 2016-07-27
  • 2016-03-06
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多