我有一个窗格,里面有一个矩形形状。 窗格有红色背景。我想要的是,在放置矩形的部分,背景是透明的。
这是代码:
Pane pane=new Pane();
pane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY)));
Rectangle rect=new Rectangle(50,50,50,50, Color.YELLOW);
pane.getChildren().add(rect);
这是结果
这就是我想要的
在第二张图片中,灰色是舞台的背景色。
我尝试使用混合模式,但我没有成功。
答案 0 :(得分:0)
要实现目标,您可以尝试使用Rectangle
的2个实例,而不是Pane
和Rectangle
。通过这样做,您可以使用Shape
类的静态子方法将这两个区域相互减去:
Shape.substract(Rect_1, Rect_2);
如Oracle所述,该方法完全符合您的要求:
public static Shape subtract(Shape shape1,
Shape shape2)
Returns a new Shape which is created by subtracting the specified second shape from the first shape.
The operation works with geometric areas occupied by the input shapes. For a single Shape such area includes the area occupied by the fill if the shape has a non-null fill and the area occupied by the stroke if the shape has a non-null stroke. So the area is empty for a shape with null stroke and null fill. The area of an input shape considered by the operation is independent on the type and configuration of the paint used for fill or stroke. Before the final operation the areas of the input shapes are transformed to the parent coordinate space of their respective topmost parent nodes.
The resulting shape will include areas that were contained only in the first shape and not in the second shape.
shape1 - shape2 = result
+----------------+ +----------------+ +----------------+
|################| |################| | |
|############## | | ##############| |## |
|############ | | ############| |#### |
|########## | | ##########| |###### |
|######## | | ########| |######## |
|###### | | ######| |###### |
|#### | | ####| |#### |
|## | | ##| |## |
+----------------+ +----------------+ +----------------+
Parameters:
shape1 - the first shape
shape2 - the second shape
Returns:
the created Shape