KickJava   Java API By Example, From Geeks To Geeks.

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


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

19 class QuadIterator implements PathIterator JavaDoc {
20     QuadCurve2D JavaDoc quad;
21     AffineTransform JavaDoc affine;
22     int index;
23
24     QuadIterator(QuadCurve2D JavaDoc q, AffineTransform JavaDoc at) {
25     this.quad = 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("quad iterator iterator out of bounds");
77     }
78     int type;
79     if (index == 0) {
80         coords[0] = (float) quad.getX1();
81         coords[1] = (float) quad.getY1();
82         type = SEG_MOVETO;
83     } else {
84         coords[0] = (float) quad.getCtrlX();
85         coords[1] = (float) quad.getCtrlY();
86         coords[2] = (float) quad.getX2();
87         coords[3] = (float) quad.getY2();
88         type = SEG_QUADTO;
89     }
90     if (affine != null) {
91         affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 2);
92     }
93     return type;
94     }
95
96     /**
97      * Returns the coordinates and type of the current path segment in
98      * the iteration.
99      * The return value is the path segment type:
100      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
101      * A double array of length 6 must be passed in and may be used to
102      * store the coordinates of the point(s).
103      * Each point is stored as a pair of double x,y coordinates.
104      * SEG_MOVETO and SEG_LINETO types will return one point,
105      * SEG_QUADTO will return two points,
106      * SEG_CUBICTO will return 3 points
107      * and SEG_CLOSE will not return any points.
108      * @see #SEG_MOVETO
109      * @see #SEG_LINETO
110      * @see #SEG_QUADTO
111      * @see #SEG_CUBICTO
112      * @see #SEG_CLOSE
113      */

114     public int currentSegment(double[] coords) {
115     if (isDone()) {
116         throw new NoSuchElementException("quad iterator iterator out of bounds");
117     }
118     int type;
119     if (index == 0) {
120         coords[0] = quad.getX1();
121         coords[1] = quad.getY1();
122         type = SEG_MOVETO;
123     } else {
124         coords[0] = quad.getCtrlX();
125         coords[1] = quad.getCtrlY();
126         coords[2] = quad.getX2();
127         coords[3] = quad.getY2();
128         type = SEG_QUADTO;
129     }
130     if (affine != null) {
131         affine.transform(coords, 0, coords, 0, index == 0 ? 1 : 2);
132     }
133     return type;
134     }
135 }
136
Popular Tags