Java Reference
In-Depth Information
Example 11•14: Spiral.java (continued)
}
/**
* This inner class is the PathIterator for our Spiral shape. For
* simplicity, it does not describe the spiral path in terms of Bezier
* curve segments, but simply approximates it with line segments. The
* flatness property specifies how far the approximation is allowed to
* deviate from the true curve.
**/
class SpiralIterator implements PathIterator {
AffineTransform transform; // How to transform generated coordinates
double flatness; // How close an approximation
double angle = startAngle; // Current angle
double radius = startRadius; // Current radius
boolean done = false;
// Are we done yet?
/** A simple constructor. Just store the parameters into fields */
public SpiralIterator(AffineTransform transform, double flatness) {
this.transform = transform;
this.flatness = flatness;
}
/**
* All PathIterators have a "winding rule" that helps to specify what
* is the inside of a area and what is the outside. If you fill a
* spiral (which you're not supposed to do) the winding rule returned
* here yields better results than the alternative, WIND_EVEN_ODD
**/
public int getWindingRule() { return WIND_NON_ZERO; }
/** Returns true if the entire path has been iterated */
public boolean isDone() { return done; }
/**
* Store the coordinates of the current segment of the path into the
* specified array, and return the type of the segment. Use
* trigonometry to compute the coordinates based on the current angle
* and radius. If this was the first point, return a MOVETO segment,
* otherwise return a LINETO segment. Also, check to see if we're done.
**/
public int currentSegment(float[] coords) {
// given the radius and the angle, compute the point coords
coords[0] = (float)(centerX + radius*Math.cos(angle));
coords[1] = (float)(centerY - radius*Math.sin(angle));
// If a transform was specified, use it on the coordinates
if (transform != null) transform.transform(coords, 0, coords, 0,1);
// If we've reached the end of the spiral remember that fact
if (angle == endAngle) done = true;
// If this is the first point in the spiral then move to it
if (angle == startAngle) return SEG_MOVETO;
// Otherwise draw a line from the previous point to this one
return SEG_LINETO;
}
Search WWH ::




Custom Search