JavaFX - 使用 Shape.union() 后保持形状边框

JavaFX - Maintain Shape Borders after using Shape.union()

有没有办法在使用 union 函数后在 JavaFX Shape 上保持 borders/strokes?例如这里是我的代码:

Shape rect = new Rectangle(150, 150);
rect.setFill(Color.WHITE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(4);

Line line = new Line(0, 40, 150, 40);
line.setStrokeWidth(2);

Shape combined = Shape.union(line, rect);
combined.setFill(Color.WHITE);
combined.setStroke(Color.BLACK);
pane.getChildren().add(combined);

预期输出:

实际输出:

有什么方法可以将两者结合在一起,以便我可以将它们拖放到一起?

您的问题是 combined.setFill(Color.WHITE);,因为它清除了所有以前的 shape 更改。

尝试这样的事情

        Line line = new Line(0, 40, 150, 40);
        Shape rect = new Rectangle(150, 150);

        Shape combined = Shape.subtract(rect,line);
        combined.setFill(Color.WHITE);
        combined.setStroke(Color.BLACK);

        rect.setFill(Color.WHITE);
        rect.setStroke(Color.BLACK);
        rect.setStrokeWidth(4);

        line.setStrokeWidth(2);
        line.setStroke(Color.BLACK);
        line.setFill(Color.BLACK);

        pane.getChildren().add(combined);

输出会像这样

有关 shape.union,subtract,intersect 的更多信息,请转到 here