gpt4 book ai didi

Java Path2D.closePath 无法正常工作?

转载 作者:行者123 更新时间:2023-12-01 09:55:05 26 4
gpt4 key购买 nike

当我得知 Java 的 Path2D.FloatclosePath 方法实际上并没有正确关闭路径时,我感到很困惑。代码如下:

Path2D.Float p = new Path2D.Float();
p.moveTo(3, 3);
p.lineTo(10, 3);
p.lineTo(8, 5);
p.closePath();

PathIterator it = p.getPathIterator(null);

float lastx = 0;
float lasty = 0;
boolean first = true;
while (it.isDone() == false) {
float[] coordinates = new float[2];
int type = it.currentSegment(coordinates);
if (first) {
first = false;
} else {
System.out.println("Segment from "+lastx+", "+lasty+" to "+coordinates[0]+", "+coordinates[1]);
}
lastx = coordinates[0];
lasty = coordinates[1];
it.next();
}

产生以下输出:

Segment from 3.0, 3.0 to 10.0, 3.0
Segment from 10.0, 3.0 to 8.0, 5.0
Segment from 8.0, 5.0 to 0.0, 0.0

但是,人们会期望 closePath 关闭到坐标 3, 3 的路径,如文档中所述:

Closes the current subpath by drawing a straight line back to the coordinates of the last moveTo. If the path is already closed then this method has no effect. (https://docs.oracle.com/javase/8/docs/api/java/awt/geom/Path2D.html#closePath--)

closePath 替换为 lineTo 到起始坐标会生成所需的线段,但是这样最后一个线段的线段类型不等于 SEG_CLOSE:(https://docs.oracle.com/javase/8/docs/api/java/awt/geom/PathIterator.html)

Type: 0 // SEG_MOVETO
Type: 1 // SEG_LINETO
Segment from 3.0, 3.0 to 10.0, 3.0
Type: 1
Segment from 10.0, 3.0 to 8.0, 5.0
Type: 1
Segment from 8.0, 5.0 to 3.0, 3.0

再次追加另一个 closePath 调用会产生不正确的结果:

Type: 0
Type: 1
Segment from 3.0, 3.0 to 10.0, 3.0
Type: 1
Segment from 10.0, 3.0 to 8.0, 5.0
Type: 1
Segment from 8.0, 5.0 to 3.0, 3.0
Type: 4 // SEG_CLOSE
Segment from 3.0, 3.0 to 0.0, 0.0 // <- not the correct coordinates!

任何人都可以复制此内容,或者以其他方式解释我缺少的内容(如果这不是错误)吗?

附加信息:操作系统:Mac OS X 10.10.5JDK:jdk1.8.0_92

亲切的问候

最佳答案

当您定义 Path2D 时,它本质上会存储一个未解释的方法调用列表,这些方法调用首先用于定义路径,因此 closePath()不做任何几何逻辑。它记录了 closePath() 被调用,但不会在路径的内部点数组中存储任何其他点,因为这是不必要的;迭代路径段的代码将能够记住路径的起始位置。

同样,PathIterator.currentSegment表示“SEG_CLOSE 不返回任何点”(因为 closePath() 未使用任何点调用)。由于它不返回任何点,因此您将打印出初始化坐标数组的默认零。

如果您出于某种目的手动迭代路径,则需要单独处理每种线段类型,因为它们具有不同数量的关联点。您可以这样打印路径:

float moveX = 0, moveY = 0;
for (PathIterator it = path.getPathIterator(null); !it.isDone(); it.next()) {
float[] c = new float[6];
int type = it.currentSegment(c);
switch (type) {
case PathIterator.SEG_MOVETO:
System.out.println("moveTo(" + c[0] + ", " + c[1] + ")");
moveX = c[0]; moveY = c[1];
break;
case PathIterator.SEG_LINETO:
System.out.println("lineTo(" + c[0] + ", " + c[1] + ")");
break;
case PathIterator.SEG_QUADTO:
System.out.println("quadTo(" + c[0] + ", " + c[1] + ", " + c[2] + ", " + c[3] + ")");
break;
case PathIterator.SEG_CUBICTO:
System.out.println("cubicTo(" + c[0] + ", " + c[1] + ", " + c[2] + ", " + c[3] + ", " + c[4] + ", " + c[5] + ")");
break;
case PathIterator.SEG_CLOSE:
System.out.println("closePath() (back to " + moveX + ", " + moveY + ")");
break;
}
}

如果您发送由 Graphics2D 绘制的路径,则无需担心这一点;渲染器会正确处理它。同样,路径的所有 HitTest 方法都工作正常,如 they iterate the path与上面看到的基本方法相同。

关于Java Path2D.closePath 无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37300479/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com