Java Reference
In-Depth Information
height
. The last two arguments,
arcWidth
and
arcHeight
, specify the arcs that will
be used for the corners so as to produce a rounded rectangle. Each corner is replaced
with a quarter of an oval that is
arcWidth
pixels wide and
arcHeight
pixels high.
This is illustrated in Display 18.15. To obtain corners that are arcs of circles, just make
arcWidth
and
arcHeight
equal.
paintComponent
for Panels
You can draw figures on a
JPanel
and place the
JPanel
in a
JFrame
. When defining
a
JPanel
class that contains a graphics drawing, use the method
paintComponent
instead of the method
paint
, but otherwise the details are similar to what we have
seen for
JFrame
s .
JFrame
s use the method
paint
. However,
JPanel
s—and in fact
all
JComponent
s—use the method
paintComponent
. A very simple example of using
paintComponent
with a
JPanel
is given in Display 18.16 .
If you look back at Display 17.12 in Chapter 17 , you will see that a
JPanel
is a
JComponent
, but a
JFrame
is not a
JComponent
. A
JFrame
is only a
Component
. This
is why they use different methods to paint the screen.
Action Drawings and
repaint
The program in Display 18.17 is similar to the program in Display 18.13. It draws a
happy face similar to the happy face given in Display 18.13, but with one difference:
There is a button at the bottom of the GUI that says
Click for a Wink
. When you
click that button, the left eye winks. (Remember the left eye is on your right.) Let us
see the details.
Display 18.15
A Rounded Rectangle
g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
produces:
,
width
(x
y)
arcWidth
height
arcHeight