KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nightlabs > editor2d > j2d > GeneralShapeIterator


1 /**
2  * <p> Project: com.nightlabs.gui </p>
3  * <p> Copyright: Copyright (c) 2004 </p>
4  * <p> Company: NightLabs GmbH (Germany) </p>
5  * <p> Creation Date: 10.01.2005 </p>
6  * <p> Author: Daniel Mazurek </p>
7 **/

8 package com.nightlabs.editor2d.j2d;
9
10 import java.awt.geom.AffineTransform JavaDoc;
11 import java.awt.geom.PathIterator JavaDoc;
12
13 /**
14  * This class represents the iterator for General Paths.
15  * It can be used to retrieve all of the elements in a GeneralShape.
16  * The {@link GeneralShape#getPathIterator}
17  * method is used to create a
18  * GeneralShapeIterator for a particular GeneralShape.
19  * The iterator can be used to iterator the path only once.
20  * Subsequent iterations require a new iterator.
21  *
22  * @see GeneralShape
23  */

24 class GeneralShapeIterator implements PathIterator JavaDoc {
25     int typeIdx = 0;
26     int pointIdx = 0;
27     GeneralShape path;
28     AffineTransform JavaDoc affine;
29
30     private static final int curvesize[] = {2, 2, 4, 6, 0};
31
32     /**
33      * Constructs an iterator given a GeneralShape.
34      * @see GeneralShape#getPathIterator
35      */

36     GeneralShapeIterator(GeneralShape path) {
37         this(path, null);
38     }
39
40     /**
41      * Constructs an iterator given a GeneralShape and an optional
42      * AffineTransform.
43      * @see GeneralShape#getPathIterator
44      */

45     GeneralShapeIterator(GeneralShape path, AffineTransform JavaDoc at) {
46         this.path = path;
47         this.affine = at;
48     }
49
50     /**
51      * Return the winding rule for determining the interior of the
52      * path.
53      * @see PathIterator#WIND_EVEN_ODD
54      * @see PathIterator#WIND_NON_ZERO
55      */

56     public int getWindingRule() {
57         return path.getWindingRule();
58     }
59
60     /**
61      * Tests if there are more points to read.
62      * @return true if there are more points to read
63      */

64     public boolean isDone() {
65         return (typeIdx >= path.numTypes);
66     }
67
68     /**
69      * Moves the iterator to the next segment of the path forwards
70      * along the primary direction of traversal as long as there are
71      * more points in that direction.
72      */

73     public void next() {
74         int type = path.pointTypes[typeIdx++];
75         pointIdx += curvesize[type];
76     }
77
78     /**
79      * Returns the coordinates and type of the current path segment in
80      * the iteration.
81      * The return value is the path segment type:
82      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
83      * A float array of length 6 must be passed in and may be used to
84      * store the coordinates of the point(s).
85      * Each point is stored as a pair of float x,y coordinates.
86      * SEG_MOVETO and SEG_LINETO types will return one point,
87      * SEG_QUADTO will return two points,
88      * SEG_CUBICTO will return 3 points
89      * and SEG_CLOSE will not return any points.
90      * @see PathIterator#SEG_MOVETO
91      * @see PathIterator#SEG_LINETO
92      * @see PathIterator#SEG_QUADTO
93      * @see PathIterator#SEG_CUBICTO
94      * @see PathIterator#SEG_CLOSE
95      */

96     public int currentSegment(float[] coords) {
97         int type = path.pointTypes[typeIdx];
98         int numCoords = curvesize[type];
99         if (numCoords > 0 && affine != null) {
100             affine.transform(path.pointCoords, pointIdx,
101                              coords, 0,
102                              numCoords / 2);
103         } else {
104             System.arraycopy(path.pointCoords, pointIdx, coords, 0, numCoords);
105         }
106         return type;
107     }
108
109     /**
110      * Returns the coordinates and type of the current path segment in
111      * the iteration.
112      * The return value is the path segment type:
113      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
114      * A double array of length 6 must be passed in and may be used to
115      * store the coordinates of the point(s).
116      * Each point is stored as a pair of double x,y coordinates.
117      * SEG_MOVETO and SEG_LINETO types will return one point,
118      * SEG_QUADTO will return two points,
119      * SEG_CUBICTO will return 3 points
120      * and SEG_CLOSE will not return any points.
121      * @see PathIterator#SEG_MOVETO
122      * @see PathIterator#SEG_LINETO
123      * @see PathIterator#SEG_QUADTO
124      * @see PathIterator#SEG_CUBICTO
125      * @see PathIterator#SEG_CLOSE
126      */

127     public int currentSegment(double[] coords) {
128         int type = path.pointTypes[typeIdx];
129         int numCoords = curvesize[type];
130         if (numCoords > 0 && affine != null) {
131             affine.transform(path.pointCoords, pointIdx,
132                              coords, 0,
133                              numCoords / 2);
134         } else {
135             for (int i=0; i < numCoords; i++) {
136                 coords[i] = path.pointCoords[pointIdx + i];
137             }
138         }
139         return type;
140     }
141 }
142
143
Popular Tags