如何创建仅在边框上具有阴影的JavaFX透明舞台?

时间:2014-08-27 18:09:28

标签: javafx

我已经阅读了许多关于透明度和阴影的问题,但我不认为我已经看到了这个具体问题。

我能够成功创建一个既有透明度又有阴影的窗口,但我无法弄清楚如何使颜色阴影不影响透明度颜色。

例如,以下代码创建一个灰色透明度和红色阴影的窗口。但是,红色也会影响主窗口的透明度,但我只希望阴影延伸到窗口的边框之外。

所以我得到的是:

enter image description here

但我想要的是:(手动编辑图像)

enter image description here

关于如何做到这一点的任何想法?

我的测试代码:

@Override
public void start(Stage stage) throws Exception {
    stage.initStyle(StageStyle.TRANSPARENT);

    StackPane stackPane = new StackPane();

    stackPane.setStyle(
        "-fx-background-color: rgba(255, 255, 255, 0.5);" +
        "-fx-effect: dropshadow(gaussian, red, 50, 0, 0, 0);" +
        "-fx-background-insets: 50;"
    );

    Scene scene = new Scene(stackPane, 450, 450);
    scene.setFill(Color.TRANSPARENT);

    stage.setScene(scene);

    stage.show();
}

1 个答案:

答案 0 :(得分:17)

我一直对如何实现这样的阴影效果感到好奇,因为阴影效果不会通过半透明的顶级内容显示出来。

我提出的解决方案是在阴影上使用剪裁,使其仅显示在阴影内的半透明内容之外。

bloodstage

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.*;
import org.scenicview.ScenicView;

// Java 8 code
public class ClippedShadow extends Application {

    private static final int shadowSize = 50;

    @Override public void start(final Stage stage) {
        stage.initStyle(StageStyle.TRANSPARENT);

        StackPane stackPane = new StackPane(createShadowPane());
        stackPane.setStyle(
                "-fx-background-color: rgba(255, 255, 255, 0.5);" +
                "-fx-background-insets: " + shadowSize + ";"
        );

        Scene scene = new Scene(stackPane, 450, 450);
        scene.setFill(Color.TRANSPARENT);
        stage.setScene(scene);
        stage.show();
    }

    // Create a shadow effect as a halo around the pane and not within
    // the pane's content area.
    private Pane createShadowPane() {
        Pane shadowPane = new Pane();
        // a "real" app would do this in a CSS stylesheet.
        shadowPane.setStyle(
                "-fx-background-color: white;" +
                "-fx-effect: dropshadow(gaussian, red, " + shadowSize + ", 0, 0, 0);" +
                "-fx-background-insets: " + shadowSize + ";"
        );

        Rectangle innerRect = new Rectangle();
        Rectangle outerRect = new Rectangle();
        shadowPane.layoutBoundsProperty().addListener(
                (observable, oldBounds, newBounds) -> {
                    innerRect.relocate(
                            newBounds.getMinX() + shadowSize,
                            newBounds.getMinY() + shadowSize
                    );
                    innerRect.setWidth(newBounds.getWidth() - shadowSize * 2);
                    innerRect.setHeight(newBounds.getHeight() - shadowSize * 2);

                    outerRect.setWidth(newBounds.getWidth());
                    outerRect.setHeight(newBounds.getHeight());

                    Shape clip = Shape.subtract(outerRect, innerRect);
                    shadowPane.setClip(clip);
                }
        );

        return shadowPane;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

相关

这个答案是关于半透明窗口和窗格的一些其他问题的后续问题,这些窗口和窗格有多个部分,其中一个我没有实现关于:如何在半透明窗口上获得光晕阴影效果? (这个问题)

相关问题