Repository navigation
Expand file tree
/
Copy pathSnowperson.java
More file actions
123 lines (96 loc) · 3.84 KB
/
Copy pathSnowperson.java
File metadata and controls
123 lines (96 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import java.awt.*; // Java abstract window toolkit
public class Snowperson extends Frame {
public static void main(String[] args) {
Snowperson c = new Snowperson (); // create a window
c.setSize (400, 400); // set its size
c.setBackground (Color.white); // set the background color
c.show (); // display the window
}
public void paint (Graphics g) {
Rectangle bb = getBounds ();
bb.x = 0;
bb.y = 0;
paintBox (g, bb);
}
public static void paintBox (Graphics g, Rectangle bb) {
// Mr. Snowperson
int personWidth = 30 * bb.width / 100;
int personHeight = bb.height;
int personx = bb.x + 20 * bb.width / 100;
int persony = bb.y;
Rectangle mrbox =
new Rectangle (personx, persony, personWidth, personHeight);
paintSnowperson (g, mrbox);
// Mrs. Snowperson
personWidth = 90 * personWidth / 100;
personHeight = 90 * bb.height / 100;
personx = 50 * bb.width / 100;
persony = bb.height - personHeight;
Rectangle mrsbox =
new Rectangle (personx, persony, personWidth, personHeight);
paintSnowperson (g, mrsbox);
}
public static void paintSnowperson (Graphics g, Rectangle bb) {
if (bb.height < 10) return; // refuse to draw very small snowpeople
int headheight = 20 * bb.height / 100;
int midheight = 30 * bb.height / 100;
int botheight = bb.height - midheight - headheight;
int headwidth = 30 * bb.width / 100;
int midwidth = 60 * bb.width / 100;
int headgap = (bb.width - headwidth) / 2;
int midgap = (bb.width - midwidth) / 2;
// top oval and face
g.drawOval (bb.x + headgap, bb.y, headwidth, headheight);
Rectangle headBox =
new Rectangle (bb.x + headgap, bb.y, headwidth, headheight);
paintFace (g, headBox);
// middle oval and rings
g.drawOval (bb.x + midgap, bb.y + headheight, midwidth, midheight);
Rectangle thoraxBox =
new Rectangle (bb.x + midgap, bb.y + headheight, midwidth, midheight);
paintRings (g, thoraxBox);
// bottom oval and baby snowpeople
int abdomeny = bb.y + headheight + midheight;
g.drawOval (bb.x, abdomeny, bb.width, botheight);
Rectangle abdomenBox =
new Rectangle (bb.x + bb.width/3, abdomeny, bb.width/3, botheight);
paintSnowperson (g, abdomenBox);
}
public static void paintFace (Graphics g, Rectangle bb) {
int eyeheight = 20 * bb.height / 100;
int eyewidth = 20 * bb.width / 100;
int gap = (bb.width - 2 * eyewidth) / 3;
int brow = 25 * bb.height / 100;
// just draw two eyes
g.fillOval (bb.x + gap, bb.y + brow, eyewidth, eyeheight);
g.fillOval (bb.x + 2 * gap + eyewidth, bb.y + brow, eyewidth, eyeheight);
}
public static void paintRings (Graphics g, Rectangle bb) {
// Polite methods avoid doing things like changing the current color,
// because the calling method might not like it. Before we change the
// color, we stow the old value.
Color oldColor = g.getColor ();
int ringWidth = 30 * bb.width/100;
int ring_height = 60 * bb.height/100;
int gap = (bb.width - 3 * ringWidth) / 2;
int xx = bb.x;
int yy = bb.y;
g.setColor (Color.blue);
g.drawOval (xx, yy, ringWidth, ring_height);
xx += ringWidth + gap;
g.setColor (Color.black);
g.drawOval (xx, yy, ringWidth, ring_height);
xx += ringWidth + gap;
g.setColor (Color.red);
g.drawOval (xx, yy, ringWidth, ring_height);
yy = bb.y + bb.height - ring_height;
xx = bb.x + (bb.width - 2 * ringWidth - gap) / 2;
g.setColor (Color.yellow);
g.drawOval (xx, yy, ringWidth, ring_height);
xx += ringWidth + gap;
g.setColor (Color.green);
g.drawOval (xx, yy, ringWidth, ring_height);
// before returning we restore the original color
g.setColor (oldColor);
}
}