KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)LineIterator.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 a line segment
14  * through the PathIterator interface.
15  *
16  * @version 1.10, 12/19/03
17  * @author Jim Graham
18  */

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

112     public int currentSegment(double[] coords) {
113     if (isDone()) {
114         throw new NoSuchElementException("line iterator out of bounds");
115     }
116     int type;
117     if (index == 0) {
118         coords[0] = line.getX1();
119         coords[1] = line.getY1();
120         type = SEG_MOVETO;
121     } else {
122         coords[0] = line.getX2();
123         coords[1] = line.getY2();
124         type = SEG_LINETO;
125     }
126     if (affine != null) {
127         affine.transform(coords, 0, coords, 0, 1);
128     }
129     return type;
130     }
131 }
132
Popular Tags