KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RectIterator.java 1.11 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 import java.util.*;
11
12 /**
13  * A utility class to iterate over the path segments of a rectangle
14  * through the PathIterator interface.
15  *
16  * @version 10 Feb 1997
17  * @author Jim Graham
18  */

19 class RectIterator implements PathIterator JavaDoc {
20     double x, y, w, h;
21     AffineTransform JavaDoc affine;
22     int index;
23
24     RectIterator(Rectangle2D JavaDoc r, AffineTransform JavaDoc at) {
25     this.x = r.getX();
26     this.y = r.getY();
27     this.w = r.getWidth();
28     this.h = r.getHeight();
29     this.affine = at;
30     if (w < 0 || h < 0) {
31         index = 6;
32     }
33     }
34
35     /**
36      * Return the winding rule for determining the insideness of the
37      * path.
38      * @see #WIND_EVEN_ODD
39      * @see #WIND_NON_ZERO
40      */

41     public int getWindingRule() {
42     return WIND_NON_ZERO;
43     }
44
45     /**
46      * Tests if there are more points to read.
47      * @return true if there are more points to read
48      */

49     public boolean isDone() {
50     return index > 5;
51     }
52
53     /**
54      * Moves the iterator to the next segment of the path forwards
55      * along the primary direction of traversal as long as there are
56      * more points in that direction.
57      */

58     public void next() {
59     index++;
60     }
61
62     /**
63      * Returns the coordinates and type of the current path segment in
64      * the iteration.
65      * The return value is the path segment type:
66      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
67      * A float array of length 6 must be passed in and may be used to
68      * store the coordinates of the point(s).
69      * Each point is stored as a pair of float x,y coordinates.
70      * SEG_MOVETO and SEG_LINETO types will return one point,
71      * SEG_QUADTO will return two points,
72      * SEG_CUBICTO will return 3 points
73      * and SEG_CLOSE will not return any points.
74      * @see #SEG_MOVETO
75      * @see #SEG_LINETO
76      * @see #SEG_QUADTO
77      * @see #SEG_CUBICTO
78      * @see #SEG_CLOSE
79      */

80     public int currentSegment(float[] coords) {
81     if (isDone()) {
82         throw new NoSuchElementException("rect iterator out of bounds");
83     }
84     if (index == 5) {
85         return SEG_CLOSE;
86     }
87     coords[0] = (float) x;
88     coords[1] = (float) y;
89     if (index == 1 || index == 2) {
90         coords[0] += (float) w;
91     }
92     if (index == 2 || index == 3) {
93         coords[1] += (float) h;
94     }
95     if (affine != null) {
96         affine.transform(coords, 0, coords, 0, 1);
97     }
98     return (index == 0 ? SEG_MOVETO : SEG_LINETO);
99     }
100
101     /**
102      * Returns the coordinates and type of the current path segment in
103      * the iteration.
104      * The return value is the path segment type:
105      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
106      * A double array of length 6 must be passed in and may be used to
107      * store the coordinates of the point(s).
108      * Each point is stored as a pair of double x,y coordinates.
109      * SEG_MOVETO and SEG_LINETO types will return one point,
110      * SEG_QUADTO will return two points,
111      * SEG_CUBICTO will return 3 points
112      * and SEG_CLOSE will not return any points.
113      * @see #SEG_MOVETO
114      * @see #SEG_LINETO
115      * @see #SEG_QUADTO
116      * @see #SEG_CUBICTO
117      * @see #SEG_CLOSE
118      */

119     public int currentSegment(double[] coords) {
120     if (isDone()) {
121         throw new NoSuchElementException("rect iterator out of bounds");
122     }
123     if (index == 5) {
124         return SEG_CLOSE;
125     }
126     coords[0] = x;
127     coords[1] = y;
128     if (index == 1 || index == 2) {
129         coords[0] += w;
130     }
131     if (index == 2 || index == 3) {
132         coords[1] += h;
133     }
134     if (affine != null) {
135         affine.transform(coords, 0, coords, 0, 1);
136     }
137     return (index == 0 ? SEG_MOVETO : SEG_LINETO);
138     }
139 }
140
Popular Tags