KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > geom > GeneralPathIterator


1 /*
2  * @(#)GeneralPathIterator.java 1.22 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.geom;
9
10 /**
11  * This class represents the iterator for General Paths.
12  * It can be used to retrieve all of the elements in a GeneralPath.
13  * The {@link GeneralPath#getPathIterator}
14  * method is used to create a
15  * GeneralPathIterator for a particular GeneralPath.
16  * The iterator can be used to iterator the path only once.
17  * Subsequent iterations require a new iterator.
18  *
19  * @see GeneralPath
20  *
21  * @version 10 Feb 1997
22  * @author Jim Graham
23  */

24 class GeneralPathIterator implements PathIterator JavaDoc {
25     int typeIdx = 0;
26     int pointIdx = 0;
27     GeneralPath JavaDoc 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 GeneralPath.
34      * @see GeneralPath#getPathIterator
35      */

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

45     GeneralPathIterator(GeneralPath JavaDoc 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
Popular Tags