Java Reference
In-Depth Information
Display 18.11
Drawing a Very Simple Face
(part 2 of 2)
31
public void
paint(Graphics g)
32
{
33
super
.paint(g);
34
g.drawOval(X_FACE, Y_FACE, FACE_DIAMETER, FACE_DIAMETER);
35
//Draw Eyes:
36
g.drawLine(X_RIGHT_EYE, Y_RIGHT_EYE,
37
X_RIGHT_EYE + EYE_WIDTH,Y_RIGHT_EYE);
38
g.drawLine(X_LEFT_EYE, Y_LEFT_EYE,
39
X_LEFT_EYE + EYE_WIDTH, Y_LEFT_EYE);
40
//Draw Mouth:
41
g.drawLine(X_MOUTH, Y_MOUTH, X_MOUTH + MOUTH_WIDTH, Y_MOUTH);
42
}
43
}
R
ESULTING
GUI
(X_FACE, Y_FACE)
The red box is not
shown on the screen. It
is there to help you
understand the
relationship between
the
paint
method
code and the resulting
drawing.
(X_MOUTH,Y_MOUTH)
FACE_DIAMETER
The method
paint
is called automatically, and you normally should not invoke it
in your code. If you do not redefine it, the method
paint
for a
JFrame
object simply
draws a frame border, title, and other standard features, and then asks the components
to all invoke their
paint
methods. If we did not redefine the method
paint
, then the
JFrame
would have a border and title but would contain nothing. The code in the
redefinition of
paint
explains how to draw the face. Let's look at the details.