Java Reference
In-Depth Information
Example 11•14: Spiral.java (continued)
return new Rectangle((int)(centerX-outerRadius),
(int)(centerY-outerRadius),
(int)(outerRadius*2), (int)(outerRadius*2));
}
/** Same as getBounds(), but with floating-point coordinates */
public Rectangle2D getBounds2D() {
return new Rectangle2D.Double(centerX-outerRadius, centerY-outerRadius,
outerRadius*2, outerRadius*2);
}
/**
* A spiral is an open curve, not a not a closed area; it does not have an
* inside and an outsize, so the contains() methods always return false.
**/
public boolean contains(double x, double y) { return false; }
public boolean contains(Point2D p) { return false; }
public boolean contains(Rectangle2D r) { return false; }
public boolean contains(double x, double y, double w, double h) {
return false;
}
/**
* This method is allowed to approximate if it would be too computationally
* intensive to determine an exact answer. Therefore, we check whether
* the rectangle intersects a circle of the outer radius. This is a good
* guess for a tight spiral, but less good for a "loose" spiral.
**/
public boolean intersects(double x, double y, double w, double h) {
Shape approx = new Ellipse2D.Double(centerX-outerRadius,
centerY-outerRadius,
outerRadius*2, outerRadius*2);
return approx.intersects(x, y, w, h);
}
/** This version of intersects() just calls the one above */
public boolean intersects(Rectangle2D r) {
return intersects(r.getX(), r.getY(), r.getWidth(), r.getHeight());
}
/**
* This method is the heart of all Shape implementations. It returns a
* PathIterator that describes the shape in terms of the line and curve
* segments that comprise it. Our iterator implementation approximates
* the shape of the spiral using line segments only. We pass in a
* "flatness" argument that tells it how good the approximation must be.
* (smaller numbers mean a better approximation).
*/
public PathIterator getPathIterator(AffineTransform at) {
return new SpiralIterator(at, outerRadius/500.0);
}
/**
* Return a PathIterator that describes the shape in terms of line
* segments only, with an approximation quality specified by flatness.
**/
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return new SpiralIterator(at, flatness);
Search WWH ::




Custom Search