Java Reference
In-Depth Information
15
public void
paintComponent(Graphics g)
16 {
17 //
Recover Graphics2D
18 Graphics2D g2 = (Graphics2D) g;
19
20 //
Draw the head
21 Ellipse2D.Double head =
new
Ellipse2D.Double(
5
,
10
,
100
,
150
);
22 g2.draw(head);
23
24 //
Draw the eyes
25 Line2D.Double eye1 =
new
Line2D.Double(
25
,
70
,
45
,
90
);
26 g2.draw(eye1);
27
28 Line2D.Double eye2 =
new
Line2D.Double(
85
,
70
,
65
,
90
);
29 g2.draw(eye2);
30
31 //
Draw the mouth
32 Rectangle mouth = new Rectangle(
30
,
130
,
50
,
5
);
33 g2.setColor(Color.RED);
34 g2.fill(mouth);
35
36 //
Draw the greeting
37 g2.setColor(Color.BLUE);
38 g2.drawString(
ÐHello, World!Ñ
,
5
,
175
);
39 }
40 }
ch02/faceviewer/FaceViewer.java
1
import
javax.swing.JFrame;
2
3
public class
FaceViewer
4 {
5
public static void
main(String[] args)
6 {
7 JFrame frame =
new
JFrame();
8 frame.setSize(
300
,
400
);
9 frame.setTitle(
ÐAn Alien FaceÑ
);