Java Reference
In-Depth Information
Display 18.11 shows a GUI program that displays a
JFrame
with a rather primitive face
drawn inside of it. The mouth and eyes are just straight line segments. We will soon see
how to get round eyes and a smile (and more), but the basic technique can be seen more
clearly in this simple figure. The code for drawing the face is given in the method
paint
.
Display 18.11
Drawing a Very Simple Face
(part 1 of 2)
1
import
javax.swing.JFrame;
2
import
java.awt.Graphics;
3
import
java.awt.Color;
4
public class
Face
extends
JFrame
5{
6
public static final int
WINDOW_WIDTH = 400;
7
public static final int
WINDOW_HEIGHT = 400;
8
public static final int
FACE_DIAMETER = 200;
9
public static final int
X_FACE = 100;
10
public static final int
Y_FACE = 100;
11
public static final int
EYE_WIDTH = 20;
12
public static final int
X_RIGHT_EYE = X_FACE + 55;
13
public static final int
Y_RIGHT_EYE = Y_FACE + 60;
14
public static final int
X_LEFT_EYE = X_FACE + 130;
15
public static final int
Y_LEFT_EYE = Y_FACE + 60;
16
public static final int
MOUTH_WIDTH = 100;
17
public static final int
X_MOUTH = X_FACE + 50;
18
public static final int
Y_MOUTH = Y_FACE + 150;
19
public static void
main(String[] args)
20
{
21
Face drawing =
new
Face();
22
drawing.setVisible(
true
);
23
}
24
public
Face()
25
{
26
super
("First Graphics Demo");
27
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
28
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
29
getContentPane().setBackground(Color.white);
30
}