Javafx 2 JavaFX2裁剪形状对象

Javafx 2 JavaFX2裁剪形状对象,javafx-2,shape,Javafx 2,Shape,我需要创建形状对象,其中有空的空间。下面可以看到一个例子: 目前,空白是另一个白色矩形,尽管我实际上希望它是空的——请看蓝色矩形后面。可能吗?我找不到实现这种效果的任何解决方案。 这是我的代码: package StackPaneTest; import javafx.application.Application; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; import javafx.fxml.Initializable

我需要创建形状对象,其中有空的空间。下面可以看到一个例子:

目前,空白是另一个白色矩形,尽管我实际上希望它是空的——请看蓝色矩形后面。可能吗?我找不到实现这种效果的任何解决方案。 这是我的代码:

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>

我的代码的输出如下所示:


如您所见,减去的形状从原始位置移动。我真的需要使用FXML实现此功能。

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

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

可能是一个多边形,其中两个边是耦合的(连接/连接的)?应该直接使用Shape.subtract进行操作,但我无法使其正确工作。如果它不工作,则可能是JFX中的错误。如果您不能处理它,请显示一个代码,您可以使用它。看起来,该减法是从位置x=0,y=0进行的,因为FXML中的矩形有参数(0,0,59,49)。你必须改变x,y来改变减法x0,y0的位置。之所以如此,是因为红色矩形是使用其他参数对齐的,而不是x,y。(layoutX、layoutY或translateX、translateY)将X更改为layoutX值将使layoutX为0并创建适当的减法效果。您知道将layoutX设置为0是否会导致其他并发症吗?谢谢用我的代码更新了问题。您的示例运行得很好,尽管对于FXML似乎必须做一些不同的事情。