1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| public class MyPolygon extends Shape {
/** List of points.
* For convenience only, we should be able to get them out of the PathIterator of the delegated path.
*/
private List<Point> pointList;
/** Delegated path.
*/
private GeneralPath delegated;
/** Creates a new instance.
*/
public MyPolygon() {
pointList = new LinkedList<Point2D>();
delegated = new GeneralPath();
}
/** Add some more point to the polygon.
* @param points Points to be added.
*/
public void addPoint(Point2D... points){
// Add to convenient list.
for (Point2D p : points) {
pointList.add(p);
}
// Reset path.
delegated.reset();
// Re-create shape.
Interator<Point2D> it = pointList.interator();
Point2D p = it.next();
delegated.moveTo((float)p.getX(), (float)p.getY());
while (it.hasNext()) {
p = it.next();
delegated.lineTo((float)p.getX(), (float)p.getY());
}
delegated.close();
}
// Pour toutes les autres methodes de l'interface Shape, deleger a delegated.
// ex:
/** {@inheritDoc}
*/
public boolean contains(Point2D p) {
return delegated.contains(p);
}
} |
Partager