|
Description
|
I hope this isn't in the database. If it is you
can all flame me...
Shouldnt drawRect and fillRect (in class Graphics)
take Rectangle as parameter also (not just
int, int, int, int)???
It's not really a bug but hey..
Morgoth666
(Review ID: 27824)
======================================================================
DESCRIPTION OF THE PROBLEM :
drawRect method only allows drawRect(int,int,int,int).
It should also allow drwaRect(Rectangle) similar to the drawPolygon method
CUSTOMER WORKAROUND :
Provide new method (no compatibilty issues) that takes a Rectangle customer as parameter.
(Review ID: 179897)
======================================================================
Posted Date : 2006-01-05 01:53:35.0
Suggested fix and JUnit test case supplied by jdk.dev.java.net member leouser:
(NOTE: the test case is too large to include here. Refer to attached file
618793.txt)
A DESCRIPTION OF THE FIX :
BUG ID: 4140876 --> drawRect doesn't take Rectangle as parameter
JDK: jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin
Files modified: java.awt.Graphics
Commentary( embedded in test javadoc as well ):
/*
* BUG ID: 4140876 --> drawRect doesn't take Rectangle as parameters
* RATIONALE:
* the Graphics class has a slew of methods that draw or set based off
* of a rectangle that is defined by x,y,width and height. But the Graphics
* class does not allow the common expression of a Rectangle to be used
* as a parameter. Hence every time the user must draw a Rectangle with
* the Graphics class he must call this method with an overly long idiom:
* operation( rect.x, rect.y, rect.width, rect.height );
*
* when the much shorter idiom can do the same:
* operation( rect );
*
* The original RFE for this specifies only adding 2 methods to the Graphics class.
* I have extended this RFE to 9 methods. It makes little sense to stop at 2
* when there are 7 more that can benefit directly from having a simpler
* method added next to it.
*
* Implementation in all cases forwards the Rectangles internals to the
* original rect method. This results in a very simple implementation in all
* cases.
*
* I believe that the older methods are frequently called with the:
* operation( rect.x, rect.y, rect.width, rect.height )
* idiom.
*
* I have found myself pondering why the shorter idiom doesn't exist. It
* is more intuitive to use, especially if you are using Rectangles which
* apparently for me has been frequent. Not only is it simpler to call,
* It also makes it easier to interact with objects that return Rectangles:
* EXAMPLES
* OLD:
*
* Rectangle r = g.getClipBounds();
* g.fillRect( r.x, r.y, r.width, r.height );
* //phew....
*
* NEW:
* g.fillRect( g.getClipBounds() );
* //ahhh.....
*
* RATIONALE AGAINST:
* Graphics is abstract and not final. Any Class that has subclassed Graphics
* and added any of the methods that have been added may be in conflict
* with these new methods.
*
* You can already do this with the existing methods.
*
* It adds 9 new methods to the interface. This is actually a positive vs. a
* negative in this case. We benefit in 3 ways I can view:
* 1. Easy to remember.
* 2. Easy to understand. The user can just glimpse a 'rect' method takes a Rectangle
* and it should make sense. In a certain sense these methods simplify
* the interface of the Graphics class.
* 3. Allows the user to not have to understand the internals of a Rectangle
* to interact with these methods. Or even know the methods to get the
* internals.
*
* TESTING STRATEGY:
* This is one of these cases where the results must be viewed. In almost all
* cases the tester needs to look at the Rectangles drawn on the GUI.
* If they are the same, then we are ok. As stated earlier, the new methods
* forward the internals of the Rectangle to the old rectangle methods.
* Possible problems occur if we incorrectly pass an internal at the wrong
* parameter spot.
*
* In cases where visual verification is not used the tester will be prompted
* to look to the console.
*
* Failure tests are difficult to come up with. We are really just testing
* that the new methods produce the same output as the old ones. We could
* add 6 more test methods to prove that something that shouldn't be drawn
* doesn't draw anything. ????
*
* WARNING ELIMINATION:
* I removed the cause of one warning. The drawBytes method was using a deprecated
* String constructor. I moved it to one that is not deprecated. This removed the warning.
*
*
* This Graphics test was written against
* jdk-6-rc-bin-b64-linux-i586-15_dec_2005.bin
*
* and also executed on a Suse Linux 7.3 distribution.
*
* Brian Harry
* JAN 3, 2006
*/
unified diff:
--- /home/nstuff/java6/jdk1.6.0/java/awt/Graphics.java Thu Dec 15 02:16:19 2005
+++ /home/javarefs/java/awt/Graphics.java Tue Jan 3 16:51:37 2006
@@ -291,6 +291,30 @@
*/
public abstract void clipRect(int x, int y, int width, int height);
+ /**
+ * Intersects the current clip with the Rectangle parameter.
+ * The resulting clipping area is the intersection of the current
+ * clipping area and the Rectangle parameter. If there is no
+ * current clipping area, either because the clip has never been
+ * set, or the clip has been cleared using <code>setClip(null)</code>,
+ * the Rectangle parameter becomes the new clip.
+ * This method sets the user clip, which is independent of the
+ * clipping associated with device bounds and window visibility.
+ * This method can only be used to make the current clip smaller.
+ * To set the current clip larger, use any of the setClip methods.
+ * Rendering operations have no effect outside of the clipping area.
+ * @param rect the Rectangle holding the coordinates to intersect the clip with
+ * @see #setClip(int, int, int, int)
+ * @see #setClip(Shape)
+ */
+ public void clipRect(Rectangle rect){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ clipRect(rect.x, rect.y, rect.width, rect.height);
+
+ }
+
/**
* Sets the current clip to the rectangle specified by the given
* coordinates. This method sets the user clip, which is
@@ -399,6 +423,29 @@
public abstract void fillRect(int x, int y, int width, int height);
/**
+ * Fills the Rectangle parameter.
+ * The left and right edges of the rectangle are at
+ * <code>x</code> and <code>x + width - 1</code>.
+ * The top and bottom edges are at
+ * <code>y</code> and <code>y + height - 1</code>.
+ * The resulting rectangle covers an area
+ * <code>width</code> pixels wide by
+ * <code>height</code> pixels tall.
+ * The rectangle is filled using the graphics context's current color.
+ * The coordinats are taken from the Rectangle parameter.
+ * @param rect the Rectangle holding the coordinates to be filled.
+ * @see java.awt.Graphics#clearRect
+ * @see java.awt.Graphics#drawRect
+ */
+ public void fillRect(Rectangle rect){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ fillRect(rect.x, rect.y, rect.width, rect.height);
+
+ }
+
+ /**
* Draws the outline of the specified rectangle.
* The left and right edges of the rectangle are at
* <code>x</code> and <code>x + width</code>.
@@ -428,6 +475,26 @@
drawLine(x, y + height, x, y + 1);
}
}
+
+ /**
+ * Draws the outline of the Rectangle parameter.
+ * The left and right edges of the rectangle are at
+ * <code>x</code> and <code>x + width</code>.
+ * The top and bottom edges are at
+ * <code>y</code> and <code>y + height</code>.
+ * The rectangle is drawn using the graphics context's current color.
+ * Coordinates are taken from the rect parameter.
+ * @param rect the Rectangle containg the coordinates to be drawn.
+ * @see java.awt.Graphics#fillRect
+ * @see java.awt.Graphics#clearRect
+ */
+ public void drawRect(Rectangle rect){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ drawRect( rect.x, rect.y, rect.width, rect.height );
+
+ }
/**
* Clears the specified rectangle by filling it with the background
@@ -451,6 +518,30 @@
public abstract void clearRect(int x, int y, int width, int height);
/**
+ * Clears the Rectangle by filling it with the background
+ * color of the current drawing surface. This operation does not
+ * use the current paint mode.
+ * <p>
+ * Beginning with Java 1.1, the background color
+ * of offscreen images may be system dependent. Applications should
+ * use <code>setColor</code> followed by <code>fillRect</code> to
+ * ensure that an offscreen image is cleared to a specific color.
+ * @param rect the Rectangle to clear
+ * @see java.awt.Graphics#fillRect(int, int, int, int)
+ * @see java.awt.Graphics#drawRect
+ * @see java.awt.Graphics#setColor(java.awt.Color)
+ * @see java.awt.Graphics#setPaintMode
+ * @see java.awt.Graphics#setXORMode(java.awt.Color)
+ */
+ public void clearRect(Rectangle rect){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ clearRect(rect.x, rect.y, rect.width, rect.height);
+
+ }
+
+ /**
* Draws an outlined round-cornered rectangle using this graphics
* context's current color. The left and right edges of the rectangle
* are at <code>x</code> and <code>x + width</code>,
@@ -470,6 +561,28 @@
int arcWidth, int arcHeight);
/**
+ * Draws an outlined round-cornered rectangle using this graphics
+ * context's current color. The left and right edges of the rectangle
+ * are at <code>x</code> and <code>x + width</code>,
+ * respectively. The top and bottom edges of the rectangle are at
+ * <code>y</code> and <code>y + height</code>.
+ * These coordinates are taken from the Rectangle parameter.
+ * @param rect the Rectangle holding the coordinates to be drawn.
+ * @param arcWidth the horizontal diameter of the arc
+ * at the four corners.
+ * @param arcHeight the vertical diameter of the arc
+ * at the four corners.
+ * @see java.awt.Graphics#fillRoundRect
+ */
+ public void drawRoundRect(Rectangle rect, int arcWidth, int arcHeight){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ drawRoundRect(rect.x, rect.y, rect.width, rect.height, arcWidth, arcHeight);
+
+ }
+
+ /**
* Fills the specified rounded corner rectangle with the current color.
* The left and right edges of the rectangle
* are at <code>x</code> and <code>x + width - 1</code>,
@@ -488,6 +601,28 @@
public abstract void fillRoundRect(int x, int y, int width, int height,
int arcWidth, int arcHeight);
+ /**
+ * Fills the specified rounded corner rectangle with the current color.
+ * The left and right edges of the rectangle
+ * are at <code>x</code> and <code>x + width - 1</code>,
+ * respectively. The top and bottom edges of the rectangle are at
+ * <code>y</code> and <code>y + height - 1</code>.
+ * The coordinates are taken from the Rectangle parameter.
+ * @param rect the Rectangle containing the coordinates to be filled.
+ * @param arcWidth the horizontal diameter
+ * of the arc at the four corners.
+ * @param arcHeight the vertical diameter
+ * of the arc at the four corners.
+ * @see java.awt.Graphics#drawRoundRect
+ */
+ public void fillRoundRect(Rectangle rect, int arcWidth, int arcHeight){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ fillRoundRect(rect.x, rect.y, rect.width, rect.height, arcWidth, arcHeight);
+
+ }
+
/**
* Draws a 3-D highlighted outline of the specified rectangle.
* The edges of the rectangle are highlighted so that they
@@ -523,6 +658,30 @@
}
/**
+ * Draws a 3-D highlighted outline of the Rectangle parameter.
+ * The edges of the rectangle are highlighted so that they
+ * appear to be beveled and lit from the upper left corner.
+ * <p>
+ * The colors used for the highlighting effect are determined
+ * based on the current color.
+ * The resulting rectangle covers an area that is
+ * <code>width + 1</code> pixels wide
+ * by <code>height + 1</code> pixels tall.
+ * @param rect the Rectangle holding the coordinates to be drawn.
+ * @param raised a boolean that determines whether the rectangle
+ * appears to be raised above the surface
+ * or sunk into the surface.
+ * @see java.awt.Graphics#fill3DRect
+ */
+ public void draw3DRect(Rectangle rect, boolean raised){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ draw3DRect(rect.x, rect.y, rect.width, rect.height, raised);
+
+ }
+
+ /**
* Paints a 3-D highlighted rectangle filled with the current color.
* The edges of the rectangle will be highlighted so that it appears
* as if the edges were beveled and lit from the upper left corner.
@@ -554,7 +713,27 @@
drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
drawLine(x + width - 1, y, x + width - 1, y + height - 2);
setColor(c);
- }
+ }
+
+ /**
+ * Paints a 3-D highlighted rectangle filled with the current color.
+ * The edges of the rectangle will be highlighted so that it appears
+ * as if the edges were beveled and lit from the upper left corner.
+ * The colors used for the highlighting effect will be determined from
+ * the current color.
+ * @param rect the Rectangle holding the coordinates to be filled.
+ * @param raised a boolean value that determines whether the
+ * rectangle appears to be raised above the surface
+ * or etched into the surface.
+ * @see java.awt.Graphics#draw3DRect
+ */
+ public void fill3DRect(Rectangle rect, boolean raised){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ fill3DRect(rect.x, rect.y, rect.width, rect.height, raised);
+
+ }
/**
* Draws the outline of an oval.
@@ -826,7 +1005,7 @@
* @see java.awt.Graphics#drawString
*/
public void drawBytes(byte data[], int offset, int length, int x, int y) {
- drawString(new String(data, 0, offset, length), x, y);
+ drawString(new String(data, offset, length), x, y);
}
/**
@@ -1202,6 +1381,38 @@
return true;
}
return clipRect.intersects(x, y, width, height);
+ }
+
+
+ /**
+ * Returns true if the specified Rectangle might intersect
+ * the current clipping area.
+ * The coordinates of the Rectangle are in the
+ * user coordinate space and are relative to the coordinate
+ * system origin of this graphics context.
+ * This method may use an algorithm that calculates a result quickly
+ * but which sometimes might return true even if the specified
+ * rectangular area does not intersect the clipping area.
+ * The specific algorithm employed may thus trade off accuracy for
+ * speed, but it will never return false unless it can guarantee
+ * that the Rectangle does not intersect the
+ * current clipping area.
+ * The clipping area used by this method can represent the
+ * intersection of the user clip as specified through the clip
+ * methods of this graphics context as well as the clipping
+ * associated with the device or image bounds and window visibility.
+ *
+ * @param rect the Rectangle to test against the clip
+ * @return <code>true</code> if the Rectangle intersects
+ * the bounds of the current clip; <code>false</code>
+ * otherwise.
+ */
+ public boolean hitClip(Rectangle rect){
+
+ if(rect == null)
+ throw new NullPointerException("rect");
+ return hitClip(rect.x, rect.y, rect.width, rect.height);
+
}
Posted Date : 2006-01-05 01:53:35.0
|