ptg7068951
Drawing Lines and Shapes 331
Drawing Rectangles
Rectangles can be filled or unfilled and have rounded or square corners.
They are created using the Rectangle2D.Float(int, int, int, int)
constructor with these arguments:
. The x coordinate at the upper left of the rectangle
. The y coordinate at upper left
. The width of the rectangle
. The height
The following statement draws an unfilled rectangle with square corners:
Rectangle2D.Float box = new
Rectangle2D.Float(245F, 65F, 20F, 10F);
This statement creates a rectangle with its upper-left corner at the (x,y)
coordinate (245,65) with a width of 20 pixels and a height of 10. To draw
this rectangle as an outline, you could use the following statement:
comp2D.draw(box);
If you want to make the rectangle filled in, use the fill()method instead:
comp.fill(box);
You can create rectangles with rounded corners instead of square ones by
using the RoundRectangle2D.Floatclass.
The constructor to this class starts with the same four arguments as the
Rectangle2D.Floatclass and adds the following two arguments:
. A number of pixels in the x direction away from the corner of the rec-
tangle
. A number of pixels in the y direction away from the corner
These distances are used to determine where the rounding of the rectan-
gle’s corner should begin.
The following statement creates a rounded rectangle:
RoundRectangle2D.Float ro = new RoundRectangle.Float(
10F, 10F,
100F, 80F,
15F, 15F);