KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)CubicIterator.java 1.9 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 cubic curve
14  * segment through the PathIterator interface.
15  *
16  * @version 10 Feb 1997
17  * @author Jim Graham
18  */

19 class CubicIterator implements PathIterator JavaDoc {
20     CubicCurve2D JavaDoc cubic;
21     AffineTransform JavaDoc affine;
22     int index;
23
24     CubicIterator(CubicCurve2D JavaDoc q, AffineTransform JavaDoc at) {
25     this.cubic = q;
26     this.affine = at;
27     }
28
29     /**
30      * Return the winding rule for determining the insideness of the
31      * path.
32      * @see #WIND_EVEN_ODD
33      * @see #WIND_NON_ZERO
34      */

35     public int getWindingRule() {
36     return WIND_NON_ZERO;
37     }
38
39     /**
40      * Tests if there are more points to read.
41      * @return true if there are more points to read
42      */

43     public boolean isDone() {
44     return (index > 1);
45     }
46
47     /**
48      * Moves the iterator to the next segment of the path forwards
49      * along the primary direction of traversal as long as there are
50      * more points in that direction.
51      */

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

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

116     public int currentSegment(double[] coords) {
117     if (isDone()) {
118         throw new NoSuchElementException("cubic iterator iterator out of bounds");
119     }
120     int type;
121     if (index == 0) {
122         coords[0] = cubic.getX1();
123         coords[1] = cubic.getY1();
124         type = SEG_MOVETO;
125     } else {
126         coords[0] = cubic.getCtrlX1();
127         coords[1] = cubic.getCtrlY1();
128         coords[2] = cubic.getCtrlX2();
129         coords[3] = cubic.getCtrlY2();
130         coords[4] = cubic.getX2();
131         coords[5] = cubic.getY2();
132         type = SEG_CUBICTO;
133     }
134     if (affine != null) {
135         affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 3);
136     }
137     return type;
138     }
139 }
140
Popular Tags