|
Description
|
Search for "++++" notes:
+++++++++++++++++
the getClipBounds() method states it can return null...
+++++++++++++++++
/**
* Returns the bounding rectangle of the current clipping area.
* This method refers to the user clip, which is independent of the
* clipping associated with device bounds and window visibility.
* If no clip has previously been set, or if the clip has been
* cleared using <code>setClip(null)</code>, this method returns
* <code>null</code>.
* The coordinates in the rectangle are relative to the coordinate
* system origin of this graphics context.
* @return the bounding rectangle of the current clipping area,
* or <code>null</code> if no clip is set.
* @see java.awt.Graphics#getClip
* @see java.awt.Graphics#clipRect
* @see java.awt.Graphics#setClip(int, int, int, int)
* @see java.awt.Graphics#setClip(Shape)
* @since JDK1.1
*/
public abstract Rectangle getClipBounds();
+++++++++++++++++
but the hitClip and getClipBounds(Rectangle) methods don't check for null values!
+++++++++++++++++
/**
* Returns the bounding rectangle of the current clipping area.
* The coordinates in the rectangle are relative to the coordinate
* system origin of this graphics context. This method differs
* from {@link getClipBounds() getClipBounds} in that an existing
* rectangle is used instead of allocating a new one.
* This method refers to the user clip, which is independent of the
* clipping associated with device bounds and window visibility.
* If no clip has previously been set, or if the clip has been
* cleared using <code>setClip(null)</code>, this method returns the
* specified <code>Rectangle</code>.
* @param r the rectangle where the current clipping area is
* copied to. Any current values in this rectangle are
* overwritten.
* @return the bounding rectangle of the current clipping area.
*/
public Rectangle getClipBounds(Rectangle r) {
// FIXME: 1.2 beta3 placeholder, replace for beta4
Rectangle clipRect = getClipBounds();
r.x = clipRect.x;
r.y = clipRect.y;
r.width = clipRect.width;
r.height = clipRect.height;
return r;
}
////////////////////////////
/**
* Returns true if the specified rectangular area intersects
* the bounding rectangle of the current clipping area.
* The coordinates in the rectangle are relative to the coordinate
* system origin of this graphics context.
*
* @param x the x coordinate of the rectangle to test against the clip
* @param y the y coordinate of the rectangle to test against the clip
* @param width the width of the rectangle to test against the clip
* @param height the height of the rectangle to test against the clip
*/
public boolean hitClip(int x, int y, int width, int height) {
// FIXME: 1.2 beta3 placeholder, replace for beta4
return new Rectangle(x,y,width,height).intersects(getClipBounds());
}
++++++++++++++++++
what's more the intersects method of java.awt.Rectangle doesn't check for null passed to it (should return false in such a case)
++++++++++++++++++
/**
* Determines whether or not this <code>Rectangle</code> and the specified
* <code>Rectangle</code> intersect. Two rectangles intersect if
* their intersection is nonempty.
* @param r the specified <code>Rectangle</code>
* @return <code>true</code> if the specified <code>Rectangle</code>
* and this <code>Rectangle</code> insersect;
* <code>false</code> otherwise.
*/
public boolean intersects(Rectangle r) {
return !((r.x + r.width <= x) ||
(r.y + r.height <= y) ||
(r.x >= x + width) ||
(r.y >= y + height));
}
(Review ID: 94169)
======================================================================
|
|
Evaluation
|
The getClipBounds(Rectangle) method specifies the appropriate default behavior
of no user clip is set, but it needs to check for a null clip to do this right.
Unfortunately, there is not much that hitClip() can do to perform its operation
accurately if it cannot get a clip bounds. In the spirit of returning the
least surprising answer, it should probably return true in that case since
it cannot rule out the possibility that the indicated rectangle is not visible.
While correcting the default implementations of these methods is interesting,
the real answer here is that these methods need to be accurately implemented
in all of the subclasses that do the actual work for the implementation.
Those subclasses have access to information such as the bounds of the
destination drawable. Such subclasses can perform much more efficient
actions than these workaround methods in any case.
xxxxx@xxxxx 2000-09-18
|