Justin
Justin

Reputation: 886

JavaFx Transparent Stage and still Paint

I am trying to make a window that is transparent but I can still draw on and the drawings are not transparent. If i put opacity on the stage it makes everything transparent. I also have tried adding a transparency to the group but that doesn't seem to change anything. How can i accomplish this?

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

Upvotes: 0

Views: 346

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

So, based on how to make transparent scene and stage in javafx?, I updated you code, adding

secondScene.setFill(Color.TRANSPARENT);

and

newWindow.initStyle(StageStyle.TRANSPARENT);

And was able to produce...

Example...

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

}

Remember, when dealing with these kind of things, you need to make sure that every layer in the hierarchy is also transparent

Upvotes: 2

Related Questions