Java Reference
In-Depth Information
Display 18.11
Drawing a Very Simple Face
(part 2 of 2)
26
super
("First Graphics Demo");
27 setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
28 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29 getContentPane().setBackground(Color.white);
30 }
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 }
Resulting GUI
(X_FACE, Y_FACE)
The dashed 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 do not redefine the method
paint,
then the