Java Reference
In-Depth Information
PathIterator
is an interface that declares methods for retrieving details of the segments that make up
a
GeneralPath
object, so a reference to an object of type
PathIterator
encapsulates all the data defining
that path.
The argument to
getPathIterator()
is an
AffineTransform
object that is applied to the path. This
provides for the possibility that a single
GeneralPath
object may be used to create a number of different
appearances on the screen simply by applying different transformations to the same object. You might have
a
GeneralPath
object that defines a complicated object, a boat, for example. You could draw several boats
on the screen from the one object simply by applying a transform before you draw each boat to set its posi-
tion and orientation.
In Sketcher you want an iterator for the unmodified path, so you pass a default
AffineTransform
object
that does nothing to the
getPathIterator()
method.
The
PathIterator
interface declares five methods:
•
int currentSegment(double[] coords)
:
coords
is used to store data relating to the current
segment as
double
values and must have six elements to record the coordinate pairs. This is to
record coordinates for one, two, or three points, depending on the current segment type. Our case
only uses line segments so coordinates for one point are always returned. The method returns one
of the following constants defined in the
PathIterator
interface:
•
SEG_MOVETO
if the segment corresponds to a
moveTo()
operation. The coordinates of
the point moved to are returned as the first two elements of the array
coords
.
•
SEG_LINETO
if the segment corresponds to a
lineTo()
operation. The coordinates of
the end point are returned as the first two elements of the array
coords
.
•
SEG_QUADTO
if the segment corresponds to a
quadTo()
operation. The coordinates of the
control point for the quadratic segment are returned as the first two elements of the
co-
ords
array, and the end point coordinates are returned in the third and fourth elements.
•
SEG_CUBICTO
if the segment corresponds to a
curveTo()
operation. The
coords
array
contains coordinates of the first control point, the second control point, and the end point
of the cubic curve segment.
•
SEG_CLOSE
if the segment corresponds to a
closePath()
operation. No values are re-
turned in the
coords
array.
•
int currentSegment(float[] coords)
: Stores data for the current segment as
float
values.
The value returned is the same as it is in the previous method.
•
int getWindingRule()
: Returns a value identifying the winding rule in effect. The value can be
WIND_EVEN_ODD
or
WIND_NON_ZERO
.
•
void next()
: Moves the iterator to the next segment as long as there is another segment.
•
boolean isDone()
: Returns
true
if the iteration is complete and returns
false
otherwise.
You have all the tools you need to get the data on every segment in the path. You just need to get a
PathIterator
reference and use the
next()
method to go through the segments in the path. The case for an
Element.Curve
object is simple: You have only a single
moveTo()
segment that is always to (0, 0) followed
by one or more
lineTo()
segments. Even though this is a fixed pattern, you still test the return value from
the
currentSegment()
method to show how it's done and in case there are errors.
The first segment is a special case. It is always a move to (0, 0), whereas all the others are lines. Thus the
procedure is to get the first segment and discard it after verifying it is a move, and then get the remaining
segments in a loop. Here's the code to create the XML: