如何在JavaFX中的半透明背景上创建可靠的DropShadow效果

时间:2018-08-02 01:31:14

标签: javafx javafx-8

将其应用于具有半透明背景颜色的节点时,我有一种奇怪的DropShadow效果,如这张图所示:

上层节点的背景色为纯色,而另一个节点为半透明的背景色。两者具有相同的精确效果,但第二个效果太模糊。我该如何纠正?


编辑

这是一个工作示例:

public class App extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    // The drop shadow effect.
    DropShadow effect = new DropShadow();
    effect.setBlurType(BlurType.GAUSSIAN);
    effect.setColor(Color.rgb(0,0,0, .5));
    effect.setSpread(0);
    effect.setOffsetX(0);
    effect.setOffsetY(7);

    // Node with solid background color.
    Button button1 = new Button("BUTTON");
    VBox.setMargin(button1, new Insets(0, 0, 15, 0));
    button1.setStyle("-fx-background-color: rgb(238,238,238);");
    button1.setEffect(effect);

    // Node with translucent background color.
    Button button2 = new Button("BUTTON");
    VBox.setMargin(button2, new Insets(15, 0, 0, 0));
    button2.setStyle("-fx-background-color: rgba(238,238,238, .5);");
    button2.setEffect(effect);

    VBox root = new VBox(button1, button2);
    root.setStyle("-fx-background-color: white;");
    root.setAlignment(Pos.CENTER);
    root.setPadding(new Insets(15));

    Scene scene = new Scene(root);

    stage.setScene(scene);
    stage.show();
  }
}

0 个答案:

没有答案