KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RoundRectIterator.java 1.10 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 an rounded rectangle
14  * through the PathIterator interface.
15  *
16  * @version 10 Feb 1997
17  * @author Jim Graham
18  */

19 class RoundRectIterator implements PathIterator JavaDoc {
20     double x, y, w, h, aw, ah;
21     AffineTransform JavaDoc affine;
22     int index;
23
24     RoundRectIterator(RoundRectangle2D JavaDoc rr, AffineTransform JavaDoc at) {
25     this.x = rr.getX();
26     this.y = rr.getY();
27     this.w = rr.getWidth();
28     this.h = rr.getHeight();
29     this.aw = Math.min(w, Math.abs(rr.getArcWidth()));
30     this.ah = Math.min(h, Math.abs(rr.getArcHeight()));
31     this.affine = at;
32     if (aw < 0 || ah < 0) {
33         // Don't draw anything...
34
index = ctrlpts.length;
35     }
36     }
37
38     /**
39      * Return the winding rule for determining the insideness of the
40      * path.
41      * @see #WIND_EVEN_ODD
42      * @see #WIND_NON_ZERO
43      */

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

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

61     public void next() {
62     index++;
63     }
64
65     private static final double angle = Math.PI / 4.0;
66     private static final double a = 1.0 - Math.cos(angle);
67     private static final double b = Math.tan(angle);
68     private static final double c = Math.sqrt(1.0 + b * b) - 1 + a;
69     private static final double cv = 4.0 / 3.0 * a * b / c;
70     private static final double acv = (1.0 - cv) / 2.0;
71
72     // For each array:
73
// 4 values for each point {v0, v1, v2, v3}:
74
// point = (x + v0 * w + v1 * arcWidth,
75
// y + v2 * h + v3 * arcHeight);
76
private static double ctrlpts[][] = {
77     { 0.0, 0.0, 0.0, 0.5 },
78     { 0.0, 0.0, 1.0, -0.5 },
79     { 0.0, 0.0, 1.0, -acv,
80        0.0, acv, 1.0, 0.0,
81        0.0, 0.5, 1.0, 0.0 },
82     { 1.0, -0.5, 1.0, 0.0 },
83     { 1.0, -acv, 1.0, 0.0,
84        1.0, 0.0, 1.0, -acv,
85        1.0, 0.0, 1.0, -0.5 },
86     { 1.0, 0.0, 0.0, 0.5 },
87     { 1.0, 0.0, 0.0, acv,
88        1.0, -acv, 0.0, 0.0,
89        1.0, -0.5, 0.0, 0.0 },
90     { 0.0, 0.5, 0.0, 0.0 },
91     { 0.0, acv, 0.0, 0.0,
92        0.0, 0.0, 0.0, acv,
93        0.0, 0.0, 0.0, 0.5 },
94     {},
95     };
96     private static int types[] = {
97     SEG_MOVETO,
98     SEG_LINETO, SEG_CUBICTO,
99     SEG_LINETO, SEG_CUBICTO,
100     SEG_LINETO, SEG_CUBICTO,
101     SEG_LINETO, SEG_CUBICTO,
102     SEG_CLOSE,
103     };
104
105     /**
106      * Returns the coordinates and type of the current path segment in
107      * the iteration.
108      * The return value is the path segment type:
109      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
110      * A float array of length 6 must be passed in and may be used to
111      * store the coordinates of the point(s).
112      * Each point is stored as a pair of float x,y coordinates.
113      * SEG_MOVETO and SEG_LINETO types will return one point,
114      * SEG_QUADTO will return two points,
115      * SEG_CUBICTO will return 3 points
116      * and SEG_CLOSE will not return any points.
117      * @see #SEG_MOVETO
118      * @see #SEG_LINETO
119      * @see #SEG_QUADTO
120      * @see #SEG_CUBICTO
121      * @see #SEG_CLOSE
122      */

123     public int currentSegment(float[] coords) {
124     if (isDone()) {
125         throw new NoSuchElementException("roundrect iterator out of bounds");
126     }
127     double ctrls[] = ctrlpts[index];
128     int nc = 0;
129     for (int i = 0; i < ctrls.length; i += 4) {
130         coords[nc++] = (float) (x + ctrls[i + 0] * w + ctrls[i + 1] * aw);
131         coords[nc++] = (float) (y + ctrls[i + 2] * h + ctrls[i + 3] * ah);
132     }
133     if (affine != null) {
134         affine.transform(coords, 0, coords, 0, nc / 2);
135     }
136     return types[index];
137     }
138
139     /**
140      * Returns the coordinates and type of the current path segment in
141      * the iteration.
142      * The return value is the path segment type:
143      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
144      * A double array of length 6 must be passed in and may be used to
145      * store the coordinates of the point(s).
146      * Each point is stored as a pair of double x,y coordinates.
147      * SEG_MOVETO and SEG_LINETO types will return one point,
148      * SEG_QUADTO will return two points,
149      * SEG_CUBICTO will return 3 points
150      * and SEG_CLOSE will not return any points.
151      * @see #SEG_MOVETO
152      * @see #SEG_LINETO
153      * @see #SEG_QUADTO
154      * @see #SEG_CUBICTO
155      * @see #SEG_CLOSE
156      */

157     public int currentSegment(double[] coords) {
158     if (isDone()) {
159         throw new NoSuchElementException("roundrect iterator out of bounds");
160     }
161     double ctrls[] = ctrlpts[index];
162     int nc = 0;
163     for (int i = 0; i < ctrls.length; i += 4) {
164         coords[nc++] = (x + ctrls[i + 0] * w + ctrls[i + 1] * aw);
165         coords[nc++] = (y + ctrls[i + 2] * h + ctrls[i + 3] * ah);
166     }
167     if (affine != null) {
168         affine.transform(coords, 0, coords, 0, nc / 2);
169     }
170     return types[index];
171     }
172 }
173
Popular Tags