【问题标题】:How to properly subtract a shape from a Path in JavaFX如何从 JavaFX 中的路径中正确减去形状
【发布时间】:2019-08-26 20:13:36
【问题描述】:

我的 JavaFX 应用程序最多可以绘制 10 种不同类型的形状。其中一些很复杂,必须用 Path 绘制为一系列直线和圆弧。一些形状是负区域,它们将从它们重叠的任何形状中减去。

我发现从基本的 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 path subtraction


【解决方案1】:

我假设您的路径未填充(未设置填充颜色)。所以我猜你只是与轮廓描边定义的区域相交。

【讨论】:

  • 你说得对,我实际上是在将填充设置为黑色时偶然发现的。将填充设置为透明也可以。感谢您的回复。
  • @itskrude 也可以使用任意填充,将减法结果的填充设置为null。
猜你喜欢
  • 2017-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 2018-03-01
相关资源
最近更新 更多