KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.j2d;
28
29 import java.awt.geom.AffineTransform JavaDoc;
30 import java.awt.geom.PathIterator JavaDoc;
31
32 /**
33  * This class represents the iterator for General Paths.
34  * It can be used to retrieve all of the elements in a GeneralShape.
35  * The {@link GeneralShape#getPathIterator}
36  * method is used to create a
37  * GeneralShapeIterator for a particular GeneralShape.
38  * The iterator can be used to iterator the path only once.
39  * Subsequent iterations require a new iterator.
40  *
41  * @see GeneralShape
42  */

43 class GeneralShapeIterator implements PathIterator JavaDoc {
44     int typeIdx = 0;
45     int pointIdx = 0;
46     GeneralShape path;
47     AffineTransform JavaDoc affine;
48
49     private static final int curvesize[] = {2, 2, 4, 6, 0};
50
51     /**
52      * Constructs an iterator given a GeneralShape.
53      * @see GeneralShape#getPathIterator
54      */

55     GeneralShapeIterator(GeneralShape path) {
56         this(path, null);
57     }
58
59     /**
60      * Constructs an iterator given a GeneralShape and an optional
61      * AffineTransform.
62      * @see GeneralShape#getPathIterator
63      */

64     GeneralShapeIterator(GeneralShape path, AffineTransform JavaDoc at) {
65         this.path = path;
66         this.affine = at;
67     }
68
69     /**
70      * Return the winding rule for determining the interior of the
71      * path.
72      * @see PathIterator#WIND_EVEN_ODD
73      * @see PathIterator#WIND_NON_ZERO
74      */

75     public int getWindingRule() {
76         return path.getWindingRule();
77     }
78
79     /**
80      * Tests if there are more points to read.
81      * @return true if there are more points to read
82      */

83     public boolean isDone() {
84         return (typeIdx >= path.numTypes);
85     }
86
87     /**
88      * Moves the iterator to the next segment of the path forwards
89      * along the primary direction of traversal as long as there are
90      * more points in that direction.
91      */

92     public void next() {
93         int type = path.pointTypes[typeIdx++];
94         pointIdx += curvesize[type];
95     }
96
97     /**
98      * Returns the coordinates and type of the current path segment in
99      * the iteration.
100      * The return value is the path segment type:
101      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
102      * A float array of length 6 must be passed in and may be used to
103      * store the coordinates of the point(s).
104      * Each point is stored as a pair of float x,y coordinates.
105      * SEG_MOVETO and SEG_LINETO types will return one point,
106      * SEG_QUADTO will return two points,
107      * SEG_CUBICTO will return 3 points
108      * and SEG_CLOSE will not return any points.
109      * @see PathIterator#SEG_MOVETO
110      * @see PathIterator#SEG_LINETO
111      * @see PathIterator#SEG_QUADTO
112      * @see PathIterator#SEG_CUBICTO
113      * @see PathIterator#SEG_CLOSE
114      */

115     public int currentSegment(float[] coords) {
116         int type = path.pointTypes[typeIdx];
117         int numCoords = curvesize[type];
118         if (numCoords > 0 && affine != null) {
119             affine.transform(path.pointCoords, pointIdx,
120                              coords, 0,
121                              numCoords / 2);
122         } else {
123             System.arraycopy(path.pointCoords, pointIdx, coords, 0, numCoords);
124         }
125         return type;
126     }
127
128     /**
129      * Returns the coordinates and type of the current path segment in
130      * the iteration.
131      * The return value is the path segment type:
132      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
133      * A double array of length 6 must be passed in and may be used to
134      * store the coordinates of the point(s).
135      * Each point is stored as a pair of double x,y coordinates.
136      * SEG_MOVETO and SEG_LINETO types will return one point,
137      * SEG_QUADTO will return two points,
138      * SEG_CUBICTO will return 3 points
139      * and SEG_CLOSE will not return any points.
140      * @see PathIterator#SEG_MOVETO
141      * @see PathIterator#SEG_LINETO
142      * @see PathIterator#SEG_QUADTO
143      * @see PathIterator#SEG_CUBICTO
144      * @see PathIterator#SEG_CLOSE
145      */

146     public int currentSegment(double[] coords) {
147         int type = path.pointTypes[typeIdx];
148         int numCoords = curvesize[type];
149         if (numCoords > 0 && affine != null) {
150             affine.transform(path.pointCoords, pointIdx,
151                              coords, 0,
152                              numCoords / 2);
153         } else {
154             for (int i=0; i < numCoords; i++) {
155                 coords[i] = path.pointCoords[pointIdx + i];
156             }
157         }
158         return type;
159     }
160 }
161
162
Popular Tags