0

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();
Justin
  • 866
  • 1
  • 13
  • 29
  • Could something like [how to make transparent scene and stage in javafx?](https://stackoverflow.com/questions/34033119/how-to-make-transparent-scene-and-stage-in-javafx) help? – MadProgrammer Jul 07 '18 at 01:33

1 Answers1

2

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Wow, I actually tried one of those on both but i guess i never tried adding the transparent on both the scene and stage. I guess that makes sense lol. Thanks so much!! – Justin Jul 07 '18 at 18:02