2017-06-16 102 views
0

有一个空心的Java.awt.geom.Area,我怎样才能得到该区域的轮廓? 例如获取一个区域的轮廓

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)); 

回答

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; 
}