Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a JavaFX transparent stage with shadows on only the border?

Tags:

javafx

I've read through many questions on transparency and shadows, but I don't think I've seen this specific issue addressed.

I'm able to successfully create a window with both transparency and a shadow, but I can't figure out how to make the color shadow not affect the transparency color.

For example, the following code creates a window with a gray transparency and a red drop shadow. However, the red color affects the transparency of the main window as well, but I only want the shadow to extend outside the border of the windows.

So what I get is:

enter image description here

But what I want is: (manually edited image)

enter image description here

Any ideas on how to do this?

My test code:

@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();
}
like image 616
dejuknow Avatar asked Aug 27 '14 18:08

dejuknow


People also ask

How do I make a scene transparent in JavaFX?

TRANSPARENT is a Color (which is a Paint ) and is not an int . So setting the fill to the transparent color is OK. This solution should work fine unless a Control is involved in the scene, in which case the additional CSS setting discussed in other answers is also required.

Can you have multiple stages displayed in a JavaFX program?

You can have multiple stages displayed in a JavaFX program.

How do I change the stage color in JavaFX?

The simplest way to set the JavaFX Scene background color or image is by invoking the Scene 's setFill() method, which can accept a color, gradient or image pattern. A more flexible way to set the background of a scene is to set the root node's background, which can accept multiple images and fills.


Video Answer


1 Answers

I've been kind of curious for a while about how to achieve such a shadow effect where the shadow effect does not show through underneath translucent top content.

The solution I came up with was to use clipping on the shadow so that it only displays outside of the translucent content that it is shadowing.

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

Related

This answer is kind of a follow on to some other questions on translucent windows and panes which had multiple parts, one of which I did not get around to implementing regarding: How to get a halo drop shadow effect on a translucent window? (this question)

  • JavaFX effect on background
  • Frosted Glass Effect in JavaFX?
like image 176
jewelsea Avatar answered Oct 04 '22 09:10

jewelsea