Skip to content

Commit b10cd8e

Browse files
committed
radius instead of side length
1 parent d11dd8e commit b10cd8e

10 files changed

Lines changed: 65 additions & 59 deletions

ch16/BlinkingPolygon.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
import java.awt.Color;
22

33
/**
4-
* A polygon that changes colors on/off.
4+
* A polygon that periodically changes colors on/off.
55
*/
66
public class BlinkingPolygon extends RegularPolygon {
77

88
private Color onColor;
9+
private int count;
910

1011
/**
1112
* Constructs a blinking polygon.
1213
*
1314
* @param nsides the number of sides
14-
* @param length length of each side
15+
* @param radius from center to vertex
1516
* @param color initial fill color
1617
*/
17-
public BlinkingPolygon(int nsides, int length, Color color) {
18-
super(nsides, length, color);
18+
public BlinkingPolygon(int nsides, int radius, Color color) {
19+
super(nsides, radius, color);
1920
onColor = color;
21+
count = 0;
2022
}
2123

2224
@Override
2325
public void step() {
24-
if (color != onColor) {
25-
color = onColor;
26-
} else {
27-
color = Color.WHITE;
26+
// count to ten first
27+
count++;
28+
if (count >= 10) {
29+
count = 0;
30+
// toggle the color
31+
if (color != onColor) {
32+
color = onColor;
33+
} else {
34+
color = Color.WHITE;
35+
}
2836
}
2937
}
3038

ch16/Drawing.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ public Object[] getActors() {
4141
return actors.toArray();
4242
}
4343

44-
/**
45-
* Calls the step method of each actor and updates the drawing.
46-
*/
47-
public void step() {
48-
for (Actor actor : actors) {
49-
actor.step();
50-
}
51-
repaint();
52-
}
53-
5444
/**
5545
* Draws all the actors on the canvas.
5646
*
@@ -63,4 +53,14 @@ public void paint(Graphics g) {
6353
}
6454
}
6555

56+
/**
57+
* Calls the step method of each actor and updates the drawing.
58+
*/
59+
public void step() {
60+
for (Actor actor : actors) {
61+
actor.step();
62+
}
63+
repaint();
64+
}
65+
6666
}

ch16/Mole.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class Mole extends BlinkingPolygon {
1616
* @param y the Y coordinate
1717
*/
1818
public Mole(int x, int y) {
19-
super(10, 20, BROWN);
19+
super(10, 30, BROWN);
2020
translate(x, y);
2121
alive = true;
2222
}

ch16/MovingPolygon.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public class MovingPolygon extends RegularPolygon {
1010
* Constructs a moving polygon.
1111
*
1212
* @param nsides the number of sides
13-
* @param length length of each side
13+
* @param radius from center to vertex
1414
*/
15-
public MovingPolygon(int nsides, int length) {
16-
super(nsides, length);
17-
this.dx = 10;
18-
this.dy = 5;
15+
public MovingPolygon(int nsides, int radius) {
16+
super(nsides, radius);
17+
dx = 10;
18+
dy = 5;
1919
}
2020

2121
/**

ch16/RegularPolygon.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,31 @@ public RegularPolygon(int nsides) {
2424
}
2525

2626
/**
27-
* Constructs a regular polygon, given the number of sides and length of
28-
* each side.
27+
* Constructs a regular polygon, given the number of sides and the radius.
2928
*
3029
* @param nsides the number of sides
31-
* @param length length of each side
30+
* @param radius from center to vertex
3231
*/
33-
public RegularPolygon(int nsides, int length) {
34-
this(nsides, length, Color.BLACK);
32+
public RegularPolygon(int nsides, int radius) {
33+
this(nsides, radius, Color.BLACK);
3534
}
3635

3736
/**
38-
* Constructs a regular polygon, given the number of sides, the length of
39-
* each side, and fill color.
37+
* Constructs a regular polygon, given the number of sides, the radius, and
38+
* fill color.
4039
*
4140
* @param nsides the number of sides
42-
* @param length length of each side
41+
* @param radius from center to vertex
4342
* @param color initial fill color
4443
*/
45-
public RegularPolygon(int nsides, int length, Color color) {
44+
public RegularPolygon(int nsides, int radius, Color color) {
4645

4746
// validate and store arguments
4847
if (nsides < 3) {
4948
throw new IllegalArgumentException("invalid nsides");
5049
}
51-
if (length < 1) {
52-
throw new IllegalArgumentException("invalid length");
50+
if (radius < 1) {
51+
throw new IllegalArgumentException("invalid radius");
5352
}
5453
if (color == null) {
5554
throw new NullPointerException("invalid color");
@@ -61,9 +60,6 @@ public RegularPolygon(int nsides, int length, Color color) {
6160
this.ypoints = new int[nsides];
6261
this.color = color;
6362

64-
// radius of the polygon (distance from center to vertex)
65-
double radius = 0.5 * length / Math.sin(Math.PI / nsides);
66-
6763
// the angle (in radians) at each vertex
6864
double angle = 2.0 * Math.PI / nsides;
6965

@@ -81,12 +77,11 @@ public RegularPolygon(int nsides, int length, Color color) {
8177

8278
@Override
8379
public void step() {
84-
// nothing to do
80+
// do nothing
8581
}
8682

8783
@Override
8884
public void draw(Graphics g) {
89-
// System.out.println(this);
9085
g.setColor(color);
9186
g.fillPolygon(this);
9287
}

ch16/RotatingPolygon.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ public class RotatingPolygon extends RegularPolygon {
1414
private int[] y0; // original ypoints
1515

1616
/**
17-
* Constructs a moving polygon.
17+
* Constructs a rotating polygon.
1818
*
1919
* @param nsides the number of sides
20-
* @param length length of each side
20+
* @param radius from center to vertex
2121
*/
22-
public RotatingPolygon(int nsides, int length) {
23-
super(nsides, length);
22+
public RotatingPolygon(int nsides, int radius) {
23+
super(nsides, radius);
2424
center();
2525
}
2626

@@ -38,11 +38,11 @@ public void center() {
3838
for (int y : ypoints) {
3939
ysum += y;
4040
}
41-
this.xmid = (int) Math.round(xsum / npoints);
42-
this.ymid = (int) Math.round(ysum / npoints);
41+
xmid = (int) Math.round(xsum / npoints);
42+
ymid = (int) Math.round(ysum / npoints);
4343

4444
// reset the rotation direction
45-
this.angle = 359;
45+
angle = 359;
4646

4747
// save the original x and y points
4848
x0 = Arrays.copyOf(xpoints, npoints);

ch16/Sim1.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class Sim1 {
1414
public static void main(String[] args) {
1515

1616
// create some regular polygons
17-
RegularPolygon p3 = new RegularPolygon(3, 100, Color.GREEN);
18-
RegularPolygon p4 = new RegularPolygon(4, 100, Color.ORANGE);
19-
RegularPolygon p5 = new RegularPolygon(360, 1, Color.MAGENTA);
17+
RegularPolygon p3 = new RegularPolygon(3, 50, Color.GREEN);
18+
RegularPolygon p4 = new RegularPolygon(5, 50, Color.ORANGE);
19+
RegularPolygon p5 = new RegularPolygon(360, 50, Color.MAGENTA);
2020

2121
// move them out of the corner
2222
p3.translate(100, 100);

ch16/Sim2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ public class Sim2 {
1515
public static void main(String[] args) {
1616

1717
// create some regular polygons
18-
BlinkingPolygon bp = new BlinkingPolygon(3, 10, Color.BLUE);
18+
BlinkingPolygon bp = new BlinkingPolygon(3, 20, Color.BLUE);
1919
MovingPolygon mp = new MovingPolygon(8, 30);
20-
RotatingPolygon rp = new RotatingPolygon(5, 50);
20+
RotatingPolygon rp = new RotatingPolygon(5, 40);
2121

2222
// move them out of the corner
2323
bp.translate(50, 50);
@@ -48,7 +48,7 @@ public static void main(String[] args) {
4848

4949
// delay the simulation
5050
try {
51-
Thread.sleep(50);
51+
Thread.sleep(1000 / 30);
5252
} catch (InterruptedException e) {
5353
// do nothing
5454
}

ch16/Sim3.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public class Sim3 implements ActionListener {
1818
*/
1919
public Sim3() {
2020
// create some regular polygons
21-
BlinkingPolygon bp = new BlinkingPolygon(3, 10, Color.BLUE);
21+
BlinkingPolygon bp = new BlinkingPolygon(3, 20, Color.BLUE);
2222
MovingPolygon mp = new MovingPolygon(8, 30);
23-
RotatingPolygon rp = new RotatingPolygon(5, 50);
23+
RotatingPolygon rp = new RotatingPolygon(5, 40);
2424

2525
// move them out of the corner
2626
bp.translate(50, 50);
@@ -50,7 +50,7 @@ public Sim3() {
5050
*/
5151
public static void main(String[] args) {
5252
Sim3 sim = new Sim3();
53-
Timer timer = new Timer(50, sim);
53+
Timer timer = new Timer(1000 / 30, sim);
5454
timer.start();
5555
}
5656

ch16/WhackAMole.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ public WhackAMole() {
2121
// create drawing, add polygons
2222
drawing = new MoleHill(800, 600);
2323
drawing.add(new Mole(150, 200));
24-
drawing.add(new Mole(350, 250));
24+
drawing.add(new Mole(350, 200));
2525
drawing.add(new Mole(550, 200));
26+
drawing.add(new Mole(150, 400));
27+
drawing.add(new Mole(350, 400));
28+
drawing.add(new Mole(550, 400));
2629

2730
// set up the window frame
28-
JFrame frame = new JFrame("Drawing");
31+
JFrame frame = new JFrame("Whack-A-Mole");
2932
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3033
frame.setResizable(false);
3134
frame.add(drawing);
@@ -41,7 +44,7 @@ public WhackAMole() {
4144
*/
4245
public static void main(String[] args) {
4346
WhackAMole sim = new WhackAMole();
44-
Timer timer = new Timer(500, sim);
47+
Timer timer = new Timer(1000 / 30, sim);
4548
timer.start();
4649
}
4750

0 commit comments

Comments
 (0)