JavaFX effect on background

前端 未结 4 663
独厮守ぢ
独厮守ぢ 2020-11-30 01:05

I\'m using this to make a iOS-themed JavaFX2 (Java7) application with a frosted glass effect. The problem is that this code uses its effect on an ImageView. I\'d like it to

4条回答
  •  死守一世寂寞
    2020-11-30 01:35

    The visual effect that you want for OS dependent window decoration, can only be achieved through the APIs that OS provides. And thus was eliminated by StageStyle.TRANSPARENT below.

    For JavaFX content itself, you can control the visuals of the stage > scene > root pane hierarchy. Stage and scene do not (and not aimed to) support advanced stylings so were eliminated by setting as transparent below.

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        root.setStyle("-fx-background-color: null;");
        root.setPadding(new Insets(10));
    
        DoubleProperty doubleProperty = new SimpleDoubleProperty(0);
    
        Region region = new Region();
        region.styleProperty().bind(Bindings
                .concat("-fx-background-radius:20; -fx-background-color: rgba(56, 176, 209, ")
                .concat(doubleProperty)
                .concat(");"));
        region.setEffect(new DropShadow(10, Color.GREY));
    
        Slider slider = new Slider(0, 1, .3);
        doubleProperty.bind(slider.valueProperty());
    
        root.getChildren().addAll(region, slider);
    
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        Scene scene = new Scene(root, 300, 250);
        scene.setFill(Color.TRANSPARENT);
    
        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    However the drop shadow effect does not play well with alpha value of the background color. You can observe it by changing the shadow's color to another contrast one.

    Output:
    enter image description here

提交回复
热议问题