【问题标题】:Getting the coordinate pairs of a Path2D object in Java?在 Java 中获取 Path2D 对象的坐标对?
【发布时间】:2017-12-09 12:17:36
【问题描述】:

我必须在 Path2D 对象中获取每组坐标的坐标,但我不知道如何。以前我们使用多边形,所以我能够初始化两个长度为Polygon.npoints 的数组,然后将它们设置为Polygon.xpointsPolygon.ypoints 数组。现在我们使用的是 Path2D 对象,我不知道该怎么做,因为我似乎只能初始化一个 PathIterator,它将一个数组作为输入并返回段?有人能解释一下如何获取 Path2D 对象的所有坐标对吗?

【问题讨论】:

    标签: java java-2d path-iterator


    【解决方案1】:

    下面是一个示例,您如何获取一个的所有线段和坐标对 PathIterator:

    您反复调用PathIteratorcurrentSegment 方法。 在每次通话中,您都会获得一段的坐标。 请特别注意,坐标的数量取决于线段类型 (从currentSegment 方法得到的返回值)。

    public static void dump(Shape shape) {
        float[] coords = new float[6];
        PathIterator pathIterator = shape.getPathIterator(new AffineTransform());
        while (!pathIterator.isDone()) {
            switch (pathIterator.currentSegment(coords)) {
            case PathIterator.SEG_MOVETO:
                System.out.printf("move to x1=%f, y1=%f\n",
                        coords[0], coords[1]);
                break;
            case PathIterator.SEG_LINETO:
                System.out.printf("line to x1=%f, y1=%f\n",
                        coords[0], coords[1]);
                break;
            case PathIterator.SEG_QUADTO:
                System.out.printf("quad to x1=%f, y1=%f, x2=%f, y2=%f\n",
                        coords[0], coords[1], coords[2], coords[3]);
                break;
            case PathIterator.SEG_CUBICTO:
                System.out.printf("cubic to x1=%f, y1=%f, x2=%f, y2=%f, x3=%f, y3=%f\n",
                        coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
                break;
            case PathIterator.SEG_CLOSE:
                System.out.printf("close\n");
                break;
            }
            pathIterator.next();
        }    
    }
    

    您可以使用此方法转储任何Shape (因此也适用于它的实现,如RectanglePolygonEllipse2DPath2D、...)

    Shape shape = ...;
    dump(shape);
    

    【讨论】:

      猜你喜欢
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 2015-08-01
      • 2016-11-19
      相关资源
      最近更新 更多