ptg7068951
HOUR 23:Creating Java2D Graphics
This rectangle has its upper-left corner at the (10,10) coordinate. The third
and fourth arguments specify how wide and tall the rectangle should be.
In this case, it should be 100 pixels wide and 80 pixels tall.
The last two arguments to drawRoundRect()specify that all four corners
should begin rounding 15 pixels away from the corner at (10,10).
Drawing Ellipses and Circles
Yo u c a n c re a t eellipses and circles with the same class, Ellipse2D.Float,
which takes four arguments:
. The x coordinate of the ellipse
. The y coordinate of the ellipse
. Its width
. Its height
The (x,y) coordinates do not indicate a point at the center of the ellipse or
circle, as you might expect. Instead, the (x,y) coordinates, width, and
height describe an invisible rectangle inside which the ellipse fits. The (x,y)
coordinate is the upper-left corner of this rectangle. If it has the same
width and height, the ellipse is a circle.
The following statement creates a circle inside the rectangle at the (245,45)
coordinate with a height and width of 5 pixels each:
Ellipse2D.Float cir = new Ellipse2D.Float(
245F, 45F, 5F, 5F);
Drawing Arcs
Anothercircular shape you can draw in Java is an , a partial ellipse or
circle. Arcs are created using the Arc2D.Floatclass, which has a construc-
tor method with many of the same arguments. You draw the arc by speci-
fying an ellipse, the portion of the ellipse that should be visible (in
degrees), and the place the arc should begin on the ellipse.
To c re a t e a n a rc , s p e c i f y t h e f o l l o w i n g i n t e g e r a rg u m e n t s t o t h e c o n s t r u c t o r :
. The x coordinate of the invisible rectangle that the ellipse fits into
. The y coordinate of the rectangle
. The width of the rectangle