有没有办法在使用 union 函数后保持 JavaFX Shape 上的边框/笔划?例如,这是我的代码:
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
关于JavaFX - 使用 Shape.union() 后维护形状边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47134318/