1 /*
2  * Copyright (c) 1997, 1999, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */

25
26 package java.awt.geom;
27
28 import java.util.*;
29
30 /**
31  * A utility class to iterate over the path segments of a line segment
32  * through the PathIterator interface.
33  *
34  * @author      Jim Graham
35  */

36 class LineIterator implements PathIterator {
37     Line2D line;
38     AffineTransform affine;
39     int index;
40
41     LineIterator(Line2D l, AffineTransform at) {
42         this.line = l;
43         this.affine = at;
44     }
45
46     /**
47      * Return the winding rule for determining the insideness of the
48      * path.
49      * @see #WIND_EVEN_ODD
50      * @see #WIND_NON_ZERO
51      */

52     public int getWindingRule() {
53         return WIND_NON_ZERO;
54     }
55
56     /**
57      * Tests if there are more points to read.
58      * @return true if there are more points to read
59      */

60     public boolean isDone() {
61         return (index > 1);
62     }
63
64     /**
65      * Moves the iterator to the next segment of the path forwards
66      * along the primary direction of traversal as long as there are
67      * more points in that direction.
68      */

69     public void next() {
70         index++;
71     }
72
73     /**
74      * Returns the coordinates and type of the current path segment in
75      * the iteration.
76      * The return value is the path segment type:
77      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
78      * A float array of length 6 must be passed in and may be used to
79      * store the coordinates of the point(s).
80      * Each point is stored as a pair of float x,y coordinates.
81      * SEG_MOVETO and SEG_LINETO types will return one point,
82      * SEG_QUADTO will return two points,
83      * SEG_CUBICTO will return 3 points
84      * and SEG_CLOSE will not return any points.
85      * @see #SEG_MOVETO
86      * @see #SEG_LINETO
87      * @see #SEG_QUADTO
88      * @see #SEG_CUBICTO
89      * @see #SEG_CLOSE
90      */

91     public int currentSegment(float[] coords) {
92         if (isDone()) {
93             throw new NoSuchElementException("line iterator out of bounds");
94         }
95         int type;
96         if (index == 0) {
97             coords[0] = (float) line.getX1();
98             coords[1] = (float) line.getY1();
99             type = SEG_MOVETO;
100         } else {
101             coords[0] = (float) line.getX2();
102             coords[1] = (float) line.getY2();
103             type = SEG_LINETO;
104         }
105         if (affine != null) {
106             affine.transform(coords, 0, coords, 0, 1);
107         }
108         return type;
109     }
110
111     /**
112      * Returns the coordinates and type of the current path segment in
113      * the iteration.
114      * The return value is the path segment type:
115      * SEG_MOVETO, SEG_LINETO, SEG_QUADTO, SEG_CUBICTO, or SEG_CLOSE.
116      * A double array of length 6 must be passed in and may be used to
117      * store the coordinates of the point(s).
118      * Each point is stored as a pair of double x,y coordinates.
119      * SEG_MOVETO and SEG_LINETO types will return one point,
120      * SEG_QUADTO will return two points,
121      * SEG_CUBICTO will return 3 points
122      * and SEG_CLOSE will not return any points.
123      * @see #SEG_MOVETO
124      * @see #SEG_LINETO
125      * @see #SEG_QUADTO
126      * @see #SEG_CUBICTO
127      * @see #SEG_CLOSE
128      */

129     public int currentSegment(double[] coords) {
130         if (isDone()) {
131             throw new NoSuchElementException("line iterator out of bounds");
132         }
133         int type;
134         if (index == 0) {
135             coords[0] = line.getX1();
136             coords[1] = line.getY1();
137             type = SEG_MOVETO;
138         } else {
139             coords[0] = line.getX2();
140             coords[1] = line.getY2();
141             type = SEG_LINETO;
142         }
143         if (affine != null) {
144             affine.transform(coords, 0, coords, 0, 1);
145         }
146         return type;
147     }
148 }
149