2013-03-24 83 views
0

我需要创建Shape对象,其中有空白空间。可以看到一个例子: enter image description hereJavaFX 2裁剪形状对象

目前,白色空间是另一个白色的矩形,虽然我真的希望它是空的 - 看到蓝色矩形后面。那可能吗?我找不到任何解决方案来实现这种效果。 这是我的代码:

package StackPaneTest; 

import javafx.application.Application; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.fxml.Initializable; 
import javafx.scene.Group; 
import javafx.scene.Node; 
import javafx.scene.Scene; 
import javafx.scene.layout.StackPane; 
import javafx.scene.shape.Rectangle; 
import javafx.scene.shape.Shape; 
import javafx.stage.Stage; 

import java.io.IOException; 
import java.net.URL; 
import java.util.ResourceBundle; 


public class StackedPaneController extends Application 
     implements Initializable { 


    @FXML // fx:id="panelBlock" 
    private StackPane panelBlock; // Value injected by FXMLLoader 
    @FXML 
    private Rectangle body; 
    @FXML 
    private Rectangle redBox; 

    public static void main(String[] args) { 
     launch(args); 
    } 

    @Override // This method is called by the FXMLLoader when initialization is complete 
    public void initialize(URL fxmlFileLocation, ResourceBundle resources) { 
     Shape newShape = Shape.subtract(body, redBox); 
     panelBlock.getChildren().add(0, newShape); 
     panelBlock.getChildren().remove(body); 

    } 

    @Override 
    public void start(Stage primaryStage) { 
     primaryStage.setTitle("The test"); 
     Group root = new Group(); 

     try { 
      root.getChildren().add((Node) FXMLLoader.load(getClass().getResource("StackedPaneBlock.fxml"))); 
     } catch (IOException e) { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 

     Scene scene = new Scene(root, 400, 400); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 

    } 
} 

而且FXML文件:

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.paint.*?> 
<?import javafx.scene.shape.*?> 
<?import javafx.scene.text.*?> 

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml" fx:controller="StackPaneTest.StackedPaneController"> 
    <children> 
    <StackPane fx:id="panelBlock" layoutX="136.0" layoutY="74.0" prefHeight="126.0" prefWidth="185.0"> 
     <children> 
     <Rectangle fx:id="body" arcHeight="5.0" arcWidth="5.0" fill="DODGERBLUE" height="200.0" stroke="BLACK" strokeType="INSIDE" width="200.0" /> 
     <Rectangle fx:id="redBox" arcHeight="5.0" arcWidth="5.0" fill="RED" height="48.99658203125" stroke="BLACK" strokeType="INSIDE" width="59.0" /> 
     </children> 
    </StackPane> 
    </children> 
</AnchorPane> 

我的代码的输出如下: enter image description here

正如你可以看到减去形状从移动原来的位置。我真的需要使用FXML来处理这个问题。

+0

可能是一个poligon,其中两侧耦合(连接/关节)? – 2013-03-24 18:01:13

+0

它应该是直接与Shape.subtract完成,但我不能让它正常工作。 – Giannis 2013-03-24 18:13:23

+0

如果它不起作用,它可能是JFX中的一个错误。如果您无法处理它,请显示您使用的代码。 – 2013-03-24 18:21:02

回答

1

Shape.subtract应该很好地工作:

public class Test extends Application { 
    @Override 
    public void start(final Stage stage) throws Exception { 
     final Rectangle outer = new Rectangle(0,0,200,200); 
     final Rectangle inner = new Rectangle(50,50,100,100); 

     final Shape result = Shape.subtract(outer, inner); 
     result.setFill(Color.DODGERBLUE); 

     final Group group = new Group(); 
     group.getChildren().add(result); 

     stage.setScene(new Scene(group)); 
     stage.show(); 
    } 

    public static void main(String[] args) { 
     Application.launch(args); 
    } 
} 

上面的清单生成你想要的结果。如果它不起作用,请发布具体的错误消息/结果的屏幕截图。

+0

用我的代码更新了这个问题。您的示例工作正常,但FXML似乎必须完成不同的操作。 – Giannis 2013-03-24 18:58:51