Skip to content

Commit dbcdbc8

Browse files
committed
Suggesting changes
1 parent fb040b1 commit dbcdbc8

7 files changed

Lines changed: 35 additions & 37 deletions

File tree

ch16/BlinkingPolygon.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import java.awt.Color;
2+
import java.awt.Graphics;
23

34
/**
45
* A polygon that periodically changes colors on/off.
56
*/
67
public class BlinkingPolygon extends RegularPolygon {
78

8-
private Color onColor;
9+
private boolean drawFlag;
910
private int count;
1011

1112
/**
@@ -17,23 +18,24 @@ public class BlinkingPolygon extends RegularPolygon {
1718
*/
1819
public BlinkingPolygon(int nsides, int radius, Color color) {
1920
super(nsides, radius, color);
20-
onColor = color;
21+
drawFlag = true;
2122
count = 0;
2223
}
23-
24+
2425
@Override
2526
public void step() {
26-
// count to ten first
27+
// every 10 steps toggle drawFlag
2728
count++;
28-
if (count >= 10) {
29+
if (count == 10) {
2930
count = 0;
30-
// toggle the color
31-
if (color != onColor) {
32-
color = onColor;
33-
} else {
34-
color = Color.WHITE;
35-
}
31+
drawFlag = !drawFlag;
3632
}
3733
}
38-
34+
35+
@Override
36+
public void draw(Graphics g) {
37+
if (drawFlag) {
38+
super.draw(g);
39+
}
40+
}
3941
}

ch16/MovingPolygon.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,4 @@ public void step() {
5252
// move one step
5353
translate(dx, dy);
5454
}
55-
5655
}

ch16/RegularPolygon.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class RegularPolygon extends Polygon implements Actor {
2020
* @param nsides the number of sides
2121
*/
2222
public RegularPolygon(int nsides) {
23-
this(nsides, 100);
23+
this(nsides, 6);
2424
}
2525

2626
/**
@@ -47,7 +47,7 @@ public RegularPolygon(int nsides, int radius, Color color) {
4747
if (nsides < 3) {
4848
throw new IllegalArgumentException("invalid nsides");
4949
}
50-
if (radius < 1) {
50+
if (radius <= 0) {
5151
throw new IllegalArgumentException("invalid radius");
5252
}
5353
if (color == null) {
@@ -68,10 +68,10 @@ public RegularPolygon(int nsides, int radius, Color color) {
6868

6969
// compute x and y coordinates, centered around the origin
7070
for (int i = 0; i < nsides; i++) {
71-
xpoints[i] = (int) Math.round(
72-
radius * Math.cos(i * angle + rotate));
73-
ypoints[i] = (int) Math.round(
74-
radius * Math.sin(i * angle + rotate));
71+
double x = radius * Math.cos(i * angle + rotate);
72+
xpoints[i] = (int) Math.round(x);
73+
double y = radius * Math.sin(i * angle + rotate);
74+
ypoints[i] = (int) Math.round(y);
7575
}
7676
}
7777

@@ -110,5 +110,4 @@ public String toString() {
110110
str.append(']');
111111
return str.toString();
112112
}
113-
114113
}

ch16/Sim1.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ public static void main(String[] args) {
3737
frame.pack();
3838
frame.setVisible(true);
3939
}
40-
4140
}

ch16/Sim2.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,4 @@ public static void main(String[] args) {
5454
}
5555
}
5656
}
57-
5857
}

ch16/Sim3.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public Sim3() {
4343
toolkit = frame.getToolkit();
4444
}
4545

46+
@Override
47+
public void actionPerformed(ActionEvent e) {
48+
toolkit.sync();
49+
drawing.step();
50+
}
51+
4652
/**
4753
* Create and start the timer.
4854
*
@@ -53,11 +59,4 @@ public static void main(String[] args) {
5359
Timer timer = new Timer(1000 / 30, sim);
5460
timer.start();
5561
}
56-
57-
@Override
58-
public void actionPerformed(ActionEvent e) {
59-
toolkit.sync();
60-
drawing.step();
61-
}
62-
6362
}

ch16/WhackAMole.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import javax.swing.Timer;
66

77
/**
8-
* Simulates the arcade game of Whack-A-Mole.
8+
* Simulates the arcade game Whack-A-Mole.
99
* https://en.wikipedia.org/wiki/Whac-A-Mole
1010
*/
1111
public class WhackAMole implements ActionListener {
@@ -37,21 +37,22 @@ public WhackAMole() {
3737
toolkit = frame.getToolkit();
3838
}
3939

40+
@Override
41+
public void actionPerformed(ActionEvent e) {
42+
toolkit.sync();
43+
drawing.step();
44+
}
45+
4046
/**
4147
* Create and start the timer.
4248
*
4349
* @param args command-line arguments
4450
*/
4551
public static void main(String[] args) {
4652
WhackAMole sim = new WhackAMole();
53+
54+
// TODO: Can we explain the 1000 / 30 or write it so it's obvious?
4755
Timer timer = new Timer(1000 / 30, sim);
4856
timer.start();
4957
}
50-
51-
@Override
52-
public void actionPerformed(ActionEvent e) {
53-
toolkit.sync();
54-
drawing.step();
55-
}
56-
5758
}

0 commit comments

Comments
 (0)