001 package cs101.awt.geom;
002
003 import java.awt.Shape;
004
005 import java.awt.geom.Point2D;
006 import java.awt.geom.PathIterator;
007
008 /**
009 * This class provides a simple interface for listing all the knot
010 * points in an <code>java.awt.Shape</code>.
011 *
012 * @author Patrick G. Heck gus.heck@olin.edu
013 * @version $Id: PointIterator.java,v 1.7 2003/03/06 21:58:16 gus Exp $
014 */
015 public class PointIterator {
016
017 // GeneralPath ourPath;
018 PathIterator iter;
019 Point2D.Float pathStart;
020 float[] data = new float[6];
021
022 /**
023 * Creates a <code>PointIterator</code> for the specified <code>Shape</code>.
024 *
025 * @param s the <code>Shape</code> to iterate over.
026 */
027 public PointIterator(Shape s) {
028 iter = s.getPathIterator(null);
029 }
030
031 /**
032 * Test to see if the entire shape has been processed.
033 *
034 * @return <code>true</code> if all points in the shape have been returned.
035 * <code>false</code> otherwise.
036 */
037
038 public boolean hasNext() {
039 return !iter.isDone();
040 }
041
042 /**
043 * Test to see if the supplied point is the same object as the current
044 * start point for the current path. If this test fails after it has
045 * succeeded then the itterator can be assumed to have begun a new portion
046 * of a discontinuous <code>GeneralPath</code>, or similar shape.
047 *
048 * @param pt The point to test
049 */
050
051 public boolean isStart(Point2D.Float pt) {
052 return pt == pathStart;
053 }
054
055 /**
056 * Return the next point in the shape. When a path in the shape is closed,
057 * The object returned is the same object that was returned for the last
058 * moveTo such that (return value of moveTo) == (return value of close).
059 * Note that this is only useful for simple shapes that contain only one
060 * path. There is no means provided in <code>PathIterator</code> to
061 * distinguish points returned from a moveTo segments beyond the
062 * first segment (which is guaranteed by Sun to be a moveTo).
063 *
064 * @return The next point in the shape.
065 */
066 public Point2D.Float nextPoint() {
067 int segType = iter.currentSegment(data);
068 iter.next();
069 switch (segType) {
070 case PathIterator.SEG_MOVETO:
071 pathStart = new Point2D.Float(data[0],data[1]);
072 return pathStart;
073
074 case PathIterator.SEG_CLOSE:
075 return pathStart;
076
077 case PathIterator.SEG_LINETO:
078 return new Point2D.Float(data[0],data[1]);
079
080 case PathIterator.SEG_QUADTO:
081 return new Point2D.Float(data[2],data[3]);
082
083 case PathIterator.SEG_CUBICTO:
084 return new Point2D.Float(data[4],data[5]);
085
086 default:
087 throw new Error("This only hapens if Sun changes PathIterator");
088
089 }
090
091 }
092 }
093 /*
094 * $Log: PointIterator.java,v $
095 * Revision 1.7 2003/03/06 21:58:16 gus
096 * even better doc
097 *
098 * Revision 1.6 2003/03/06 21:56:54 gus
099 * better doc
100 *
101 * Revision 1.5 2003/03/06 21:54:02 gus
102 * add a method to test for equivalance with the start of the current path.
103 *
104 * Revision 1.4 2003/03/06 18:55:56 gus
105 * we need to advance the pathIterator despite what the nutshell book claims
106 *
107 * Revision 1.3 2003/03/04 16:04:45 gus
108 * Javadoc improvements
109 *
110 * Revision 1.2 2003/03/04 15:32:20 gus
111 * adding log comment
112 *
113 */