Java Reference
In-Depth Information
Segment Type
Description
SEG _ MOVETO
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
The segment corresponds to a lineTo() operation. The coordinates of
the end point of the line are returned as the first two elements of the array,
coords .
SEG _ QUADTO
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 array, coords , and the end point is returned as the third
and fourth elements.
SEG _ CUBICTO
The segment corresponds to a curveTo() operation. The array coords
will contain coordinates of the first control point, the second control point,
and the end point of the cubic curve segment.
SEG _ CLOSE
The segment corresponds to a closePath() operation. The segment
closes the path by connecting the current point to the first point in the
path. No values are returned in the coords array.
We have all the tools we need to get the data on every segment in the path. We just need to get a
PathIterator reference and use the next() method to go through it. Our case is simple: we only
have a single moveTo() segment - always to (0, 0) - followed by one or more lineTo() segments.
We will still test the return type, though, to show how it's done, and in case there are errors. We're
going to end up with an array of coordinates with an unpredictable number of elements: it sounds like a
case for a Vector , particularly since Vector objects are serializable.
A Vector object only stores object references, not the float values that we have, so we'll have to
convert our coordinate values to objects of type Float before storing them. The first segment is a
special case. It is always a move to (0, 0), whereas all the others will be lines. Thus the procedure will be
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:
private void writeObject(ObjectOutputStream out) throws IOException {
PathIterator iterator = curve.getPathIterator(new AffineTransform());
Vector coords = new Vector();
int maxCoordCount = 6;
float[] temp = new float[maxCoordCount]; // Stores segment data
int result = iterator.currentSegment(temp); // Get first segment
assert(result == iterator.SEG _ MOVETO);
iterator.next(); // Next segment
while(!iterator.isDone()) { // While we have segments
result = iterator.currentSegment(temp); // Get the segment data
assert(result == iterator.SEG _ LINETO);
coords.add(new Float(temp[0])); // Add x coordinate to Vector
coords.add(new Float(temp[1])); // Add y coordinate
Search WWH ::




Custom Search