Skip to content

Commit b395aa4

Browse files
committed
added more specialization
1 parent 70b0af8 commit b395aa4

10 files changed

Lines changed: 75 additions & 31 deletions

ch15/GridCanvas.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public void draw(Graphics g) {
105105
*
106106
* @param g graphics context
107107
*/
108-
@Override
109108
public void paint(Graphics g) {
110109
draw(g);
111110
}
@@ -117,7 +116,6 @@ public void paint(Graphics g) {
117116
*
118117
* @param g graphics context
119118
*/
120-
@Override
121119
public void update(Graphics g) {
122120
draw(g);
123121
}

ch16/BlinkingPolygon.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.awt.Color;
2+
3+
/**
4+
* A polygon that changes colors on/off.
5+
*/
6+
public class BlinkingPolygon extends RegularPolygon {
7+
8+
private Color onColor;
9+
10+
/**
11+
* Constructs a blinking polygon.
12+
*
13+
* @param nsides the number of sides
14+
* @param length length of each side
15+
* @param color initial fill color
16+
*/
17+
public BlinkingPolygon(int nsides, int length, Color color) {
18+
super(nsides, length, color);
19+
onColor = color;
20+
}
21+
22+
@Override
23+
public void act() {
24+
if (color != onColor) {
25+
color = onColor;
26+
} else {
27+
color = Color.WHITE;
28+
}
29+
}
30+
31+
}

ch16/Drawing.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,21 @@ public Object[] getActors() {
4242
}
4343

4444
/**
45-
* Calls the act method of each actor.
45+
* Calls the act method of each actor and updates the drawing.
4646
*/
47-
public void nextact() {
47+
public void next() {
4848
for (Actor actor : actors) {
4949
actor.act();
5050
}
51+
repaint();
5152
}
5253

5354
/**
5455
* Draws all the actors on the canvas.
5556
*
5657
* @param g graphics context
5758
*/
59+
@Override
5860
public void paint(Graphics g) {
5961
for (Actor actor : actors) {
6062
actor.draw(g);

ch16/Mole.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
/**
44
* A polygon that represents a small burrowing mammal.
55
*/
6-
public class Mole extends RegularPolygon {
6+
public class Mole extends BlinkingPolygon {
77

88
public static final Color BROWN = new Color(153, 102, 0);
99

10+
private boolean alive;
11+
1012
/**
1113
* Creates a mole at the given location.
1214
*
@@ -16,13 +18,26 @@ public class Mole extends RegularPolygon {
1618
public Mole(int x, int y) {
1719
super(10, 20, BROWN);
1820
translate(x, y);
21+
alive = true;
22+
}
23+
24+
@Override
25+
public void act() {
26+
// blink on/off at random times
27+
if (alive && Math.random() < 0.5) {
28+
super.act();
29+
}
1930
}
2031

2132
/**
2233
* Updates the state of the mole when clicked.
2334
*/
2435
public void whack() {
25-
setColor(Color.LIGHT_GRAY);
36+
// ignore whack when invisible
37+
if (color == BROWN) {
38+
color = Color.LIGHT_GRAY;
39+
alive = false;
40+
}
2641
}
2742

2843
}

ch16/MovingPolygon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* A polygon that moves around the screen.
2+
* A polygon that bounces off the walls.
33
*/
44
public class MovingPolygon extends RegularPolygon {
55

ch16/RegularPolygon.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class RegularPolygon extends Polygon implements Actor {
1212
"Triangle", "Square", "Pentagon", "Hexagon",
1313
"Heptagon", "Octagon", "Nonagon", "Decagon"};
1414

15-
private Color color;
15+
protected Color color;
1616

1717
/**
1818
* Constructs a regular polygon, given the number of sides.
@@ -40,7 +40,7 @@ public RegularPolygon(int nsides, int length) {
4040
*
4141
* @param nsides the number of sides
4242
* @param length length of each side
43-
* @param color the fill color
43+
* @param color initial fill color
4444
*/
4545
public RegularPolygon(int nsides, int length, Color color) {
4646

@@ -91,13 +91,6 @@ public void draw(Graphics g) {
9191
g.fillPolygon(this);
9292
}
9393

94-
/**
95-
* @param color the new color
96-
*/
97-
public void setColor(Color color) {
98-
this.color = color;
99-
}
100-
10194
@Override
10295
public String toString() {
10396
StringBuilder str = new StringBuilder();

ch16/Sim1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void main(String[] args) {
3030
drawing.add(p5);
3131

3232
// set up the window frame
33-
JFrame frame = new JFrame("Drawing");
33+
JFrame frame = new JFrame("Drawing (no animation)");
3434
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3535
frame.setResizable(false);
3636
frame.add(drawing);

ch16/Sim2.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import java.awt.Color;
12
import java.awt.Toolkit;
23
import javax.swing.JFrame;
34

45
/**
5-
* Example simulation of moving objects.
6+
* Example simulation of animated objects.
67
*/
78
public class Sim2 {
89

@@ -14,20 +15,23 @@ public class Sim2 {
1415
public static void main(String[] args) {
1516

1617
// create some regular polygons
18+
BlinkingPolygon bp = new BlinkingPolygon(3, 10, Color.BLUE);
1719
MovingPolygon mp = new MovingPolygon(8, 30);
1820
RotatingPolygon rp = new RotatingPolygon(5, 50);
1921

2022
// move them out of the corner
23+
bp.translate(50, 50);
2124
mp.translate(100, 100);
2225
rp.translate(200, 200);
2326

2427
// create drawing, add polygons
2528
Drawing drawing = new Drawing(800, 600);
29+
drawing.add(bp);
2630
drawing.add(mp);
2731
drawing.add(rp);
2832

2933
// set up the window frame
30-
JFrame frame = new JFrame("Drawing");
34+
JFrame frame = new JFrame("Drawing with Thread.sleep");
3135
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3236
frame.setResizable(false);
3337
frame.add(drawing);
@@ -39,13 +43,12 @@ public static void main(String[] args) {
3943
while (true) {
4044

4145
// update the drawing
42-
drawing.nextact();
43-
drawing.repaint();
46+
toolkit.sync();
47+
drawing.next();
4448

4549
// delay the simulation
4650
try {
47-
Thread.sleep(1000 / 60);
48-
toolkit.sync();
51+
Thread.sleep(50);
4952
} catch (InterruptedException e) {
5053
// do nothing
5154
}

ch16/Sim3.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import java.awt.Color;
12
import java.awt.Toolkit;
23
import java.awt.event.ActionEvent;
34
import java.awt.event.ActionListener;
45
import javax.swing.JFrame;
56
import javax.swing.Timer;
67

78
/**
8-
* Smoother version using a timer.
9+
* Smoother version of Sim2 using a timer.
910
*/
1011
public class Sim3 implements ActionListener {
1112

@@ -17,20 +18,23 @@ public class Sim3 implements ActionListener {
1718
*/
1819
public Sim3() {
1920
// create some regular polygons
21+
BlinkingPolygon bp = new BlinkingPolygon(3, 10, Color.BLUE);
2022
MovingPolygon mp = new MovingPolygon(8, 30);
2123
RotatingPolygon rp = new RotatingPolygon(5, 50);
2224

2325
// move them out of the corner
26+
bp.translate(50, 50);
2427
mp.translate(100, 100);
2528
rp.translate(200, 200);
2629

2730
// create drawing, add polygons
2831
drawing = new Drawing(800, 600);
32+
drawing.add(bp);
2933
drawing.add(mp);
3034
drawing.add(rp);
3135

3236
// set up the window frame
33-
JFrame frame = new JFrame("Drawing");
37+
JFrame frame = new JFrame("Drawing with ActionListener");
3438
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3539
frame.setResizable(false);
3640
frame.add(drawing);
@@ -46,15 +50,14 @@ public Sim3() {
4650
*/
4751
public static void main(String[] args) {
4852
Sim3 sim = new Sim3();
49-
Timer timer = new Timer(1000 / 60, sim);
53+
Timer timer = new Timer(50, sim);
5054
timer.start();
5155
}
5256

5357
@Override
5458
public void actionPerformed(ActionEvent e) {
55-
drawing.nextact();
56-
drawing.repaint();
5759
toolkit.sync();
60+
drawing.next();
5861
}
5962

6063
}

ch16/WhackAMole.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,14 @@ public WhackAMole() {
4141
*/
4242
public static void main(String[] args) {
4343
WhackAMole sim = new WhackAMole();
44-
Timer timer = new Timer(1000 / 60, sim);
44+
Timer timer = new Timer(500, sim);
4545
timer.start();
4646
}
4747

4848
@Override
4949
public void actionPerformed(ActionEvent e) {
50-
drawing.nextact();
51-
drawing.repaint();
5250
toolkit.sync();
51+
drawing.next();
5352
}
5453

5554
}

0 commit comments

Comments
 (0)