2017-06-24 39 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