java 使矩形透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26222469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 09:31:00  来源:igfitidea点击:

Make Rectangle transparent

javajavafx-8stagescene

提问by Stéphane GROSSMANN

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

采纳答案by Uluk Biy

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

由于 JavaFX 中的屏幕图是分层结构的,要显示桌面,您还需要使StageScene透明,并使用形状“算术”:

@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.

稍后您可以向窗格/组添加可拖动功能。

回答by James_D

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.

但我不太确定这是否是您的意思。

回答by Kamil Janyga

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

您必须更改矩形的不透明度: rect.opacityProperty().set(0.5);