2017-06-24 40 views
0

我有一个窗格,里面有一个矩形形状。 窗格有红色背景。我想要的是,在矩形放置的部分,背景是透明的。JavaFX 8 - 如何清除背景的一部分使其透明

这是代码:

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

这是结果

enter image description here

这就是我想要

enter image description here

在第二张照片中灰色是的背景颜色阶段。

我试图玩混合模式,但我没有成功。

回答

0

为了实现您的目标,您可以尝试使用Rectangle的两个实例,而不是PaneRectangle。通过这样做,你可以使用Shape类的静态。减去方法从彼此。减去两个方面:

Shape.substract(Rect_1, Rect_2); 

正如甲骨文表示,该方法做了你想做什么:

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 

Source