我很困惑地发现Java的closePathPath2D.Float方法实际上并未正确关闭路径。如下代码:

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坐标的路径:


通过画一条直线回到上一个moveTo的坐标来关闭当前子路径。如果路径已经关闭,则此方法无效。
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.5
JDK:jdk1.8.0_92

亲切的问候

最佳答案

定义Path2D时,它实际上存储了最初用于定义路径的方法调用的未解释列表,因此closePath()不会执行任何几何逻辑。它记录了closePath()被调用,但是没有在路径的内部点数组中存储任何其他点,因为这是没有必要的。迭代路径段的代码将能够记住路径的起点。

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

如果出于某种目的手动迭代路径,则需要分别处理每个细分类型,因为它们具有不同数量的关联点。您可以通过以下方式打印出路径:

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绘制的路径,则无需担心。渲染器将​​正确处理它。同样,该路径的所有命中测试方法都可以正常工作,因为they iterate the path的基本方式与您在上面看到的相同。

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

10-09 22:27