获取区域的轮廓

时间:2017-06-16 07:34:32

标签: java awt

有一个空洞的Java.awt.geom.Area,我怎样才能得到该区域的轮廓? e.g。

Ellipse2D shape1 = new Ellipse2D.Double (20, 20, 160, 160);
Ellipse2D shape2 = new Ellipse2D.Double (60, 60, 80, 80);

Area area = new Area(shape1);
area.subtract(new Area(shape2));

1 个答案:

答案 0 :(得分:0)

愚蠢的方式:

static public Area getOutline(Area area) {
    Area ret = new Area();
    double[] coords = new double[6];
    GeneralPath tmpPath = new GeneralPath();

    PathIterator pathIterator = area.getPathIterator(null);
    for ( ; !pathIterator.isDone(); pathIterator.next() ) {
        int type = pathIterator.currentSegment(coords);
        switch (type) {
        case PathIterator.WIND_EVEN_ODD:
            tmpPath.reset();
            tmpPath.moveTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_LINETO:
            tmpPath.lineTo(coords[0], coords[1]);
            break;
        case PathIterator.SEG_QUADTO:
            tmpPath.quadTo(coords[0], coords[1], coords[2], coords[3]);
            break;
        case PathIterator.SEG_CUBICTO:
            tmpPath.curveTo(coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
            break;
        case PathIterator.SEG_CLOSE:
            ret.add(new Area(tmpPath));
            break;
        default:
            System.err.println("Unhandled type " + type);
            break;
        }
    }

    return ret;
}