我的 JavaFX 应用程序绘制多达 10 种不同类型的形状。其中一些很复杂,必须用路径绘制为一系列直线和弧线。有些形状是负值区域,需要从它们重叠的任何形状中减去它们。
我发现从基本 Javafx 形状(例如圆形和矩形)中减去可以按预期工作,但从 Path 生成的形状却不能。
显示的重叠形状:
相减的结果:
正如您所看到的,形状被正确地从圆中减去,但尝试从“圆角”形状中减去椭圆只会留下一个间隙,并且线条更暗/更粗,因为它试图通过回溯来缩小该间隙到间隙的另一端,而不是绘制重叠区域。
这是绘制图像中所示的复杂形状的代码
//Here the v shape is drawn from right to left
Path path = new Path();
path.getElements().add(new MoveTo(firstLineXposition, firstLineYposition));
path.getElements().add(new LineTo(originX, originY));
path.getElements().add(new LineTo(secondLineXPos, secondLineYPos));
//here the arc is drawn from top left point to the top right point
ArcTo arc = new ArcTo();
arc.setX(firstLineXposition);
arc.setY(firstLineYposition);
arc.setRadiusX(radiusPositionX);
arc.setRadiusY(radiusPositionY);
path.getElements().add(arc);
path.getElements().add(new ClosePath());
我猜测使用 Path 绘制这些形状时遗漏了一步,它应该被视为一个完整的形状,而似乎被视为一系列线条。
最佳答案
我假设您的路径未填充(没有设置填充颜色)。所以我猜你只是与轮廓笔划定义的区域相交。
关于javafx - 如何在 JavaFX 中正确从路径中减去形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57664386/