Make Rectangle transparent
Asked Answered
M

3

5

I need to make a drawn rectangle mouse transparent, in order to see the desktop. The following code draws my rectangle. What should I add to get that ? Thanks for help

public void start(Stage primaryStage) {
    Group group = new Group();

    Rectangle rect = new Rectangle(20,20,200,200);

    rect.setArcHeight(15);
    rect.setArcWidth(15);

    rect.setStroke(Color.BLACK);
    group.getChildren().add(rect);

    Scene scene = new Scene(group, 300, 200);
    primaryStage.setScene(scene);
    primaryStage.show();
}
Meredi answered 6/10, 2014 at 18:27 Comment(1)
Can you clarify? If you want to see the desktop, you will need the window to be transparent, but perhaps that is not what you mean?Musket
M
3

Since the screen graph in JavaFX is in a hierarchical structure, to show desktop you need to also make the Stage and Scene transparent, and use shape "arithmetic":

@Override
public void start(Stage stage) {
    Group group = new Group();
    Rectangle rect = new Rectangle(0, 0, 350, 300);
    Rectangle clip = new Rectangle(20, 20, 200, 200);
    clip.setArcHeight(15);
    clip.setArcWidth(15);

    Shape shape = Shape.subtract(rect, clip);

    shape.setFill(Color.GRAY);
    group.getChildren().add(shape);
    Scene scene = new Scene(group);
    scene.setFill(Color.TRANSPARENT);
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.setScene(scene);
    stage.show();
}

Later you can add draggable feature to the pane/group.

Mcnelly answered 7/10, 2014 at 6:43 Comment(3)
Thanks for help. The draggable feature is my next aim. Should I open a new question, because I cannot get it ?Duello
@StéphaneGROSSMANN see this answer for the hint.. If it doesn't help yes you can open new question.Mcnelly
I put this #26254854 But no answer :-( Tks anywayDuello
M
5

If you just want the interior of the rectangle to be transparent, then all you need is

rect.setFill(Color.TRANSPARENT);

but I'm not quite sure if this is what you mean.

Musket answered 6/10, 2014 at 20:21 Comment(0)
M
3

Since the screen graph in JavaFX is in a hierarchical structure, to show desktop you need to also make the Stage and Scene transparent, and use shape "arithmetic":

@Override
public void start(Stage stage) {
    Group group = new Group();
    Rectangle rect = new Rectangle(0, 0, 350, 300);
    Rectangle clip = new Rectangle(20, 20, 200, 200);
    clip.setArcHeight(15);
    clip.setArcWidth(15);

    Shape shape = Shape.subtract(rect, clip);

    shape.setFill(Color.GRAY);
    group.getChildren().add(shape);
    Scene scene = new Scene(group);
    scene.setFill(Color.TRANSPARENT);
    stage.initStyle(StageStyle.TRANSPARENT);
    stage.setScene(scene);
    stage.show();
}

Later you can add draggable feature to the pane/group.

Mcnelly answered 7/10, 2014 at 6:43 Comment(3)
Thanks for help. The draggable feature is my next aim. Should I open a new question, because I cannot get it ?Duello
@StéphaneGROSSMANN see this answer for the hint.. If it doesn't help yes you can open new question.Mcnelly
I put this #26254854 But no answer :-( Tks anywayDuello
A
0

If I understand you correctly as I don't get this mouse part of 'drawn rectangle mouse transparent' - your rectangle should be transparent?

You have to change the opacity of a rectangle: rect.opacityProperty().set(0.5);

Abell answered 6/10, 2014 at 18:56 Comment(4)
if you mean this : rect.setStroke(new javafx.scene.paint.Color(0, 0, 0, 0)); turns it to blackDuello
With 1, the rectangle is black too :-(Duello
Check the edited answer. :) Sadly you cannot just apply a transparent color. You have to specify an alpha channel of a rectangle (strange, strange...). So closer to 0.0 is more transparent 1.0 is solid.Abell
Great ! That's what I wanted. Thanks a lot :-)Duello

© 2022 - 2024 — McMap. All rights reserved.