Skip to content

Commit 4e05ec0

Browse files
committed
ch17 revisions
1 parent 33b1e75 commit 4e05ec0

File tree

13 files changed

+22
-463
lines changed

13 files changed

+22
-463
lines changed

ch17/DrawablePolygon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
public class DrawablePolygon extends Polygon implements Actor {
99

10-
public Color color;
10+
protected Color color;
1111

1212
/**
1313
* Creates an empty polygon.

ch17/Drawing.bak

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import java.awt.Graphics;
44
import java.util.ArrayList;
55

66
/**
7-
* Draws a collection of actors.
7+
* Draws a collection of DrawablePolygons.
88
*/
99
public class Drawing extends Canvas {
1010

11-
private ArrayList<ColorPolygon> list;
11+
private ArrayList<DrawablePolygon> list;
1212

1313
/**
1414
* Constructs a drawing of given size.
@@ -19,35 +19,26 @@ public class Drawing extends Canvas {
1919
public Drawing(int width, int height) {
2020
setSize(width, height);
2121
setBackground(Color.WHITE);
22-
list = new ArrayList<ColorPolygon>();
22+
list = new ArrayList<DrawablePolygon>();
2323
}
2424

2525
/**
2626
* Adds a new actor to the drawing.
2727
*
2828
* @param dp the object to add
2929
*/
30-
public void add(ColorPolygon dp) {
30+
public void add(DrawablePolygon dp) {
3131
list.add(dp);
3232
}
3333

34-
/**
35-
* Gets current actors as an array.
36-
*
37-
* @return array of actor objects
38-
*/
39-
public Object[] getActors() {
40-
return list.toArray();
41-
}
42-
4334
/**
4435
* Draws all the actors on the canvas.
4536
*
4637
* @param g graphics context
4738
*/
4839
@Override
4940
public void paint(Graphics g) {
50-
for (ColorPolygon dp : list) {
41+
for (DrawablePolygon dp : list) {
5142
dp.draw(g);
5243
}
5344
}
@@ -56,7 +47,7 @@ public class Drawing extends Canvas {
5647
* Calls the step method of each actor and updates the drawing.
5748
*/
5849
public void step() {
59-
for (ColorPolygon dp : list) {
50+
for (DrawablePolygon dp : list) {
6051
dp.step();
6152
}
6253
repaint();

ch17/Sim1.java renamed to ch17/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/**
55
* Initial drawing of stationary objects.
66
*/
7-
public class Sim1 {
7+
public class Main {
88

99
/**
1010
* Test program that draws a few polygons.
@@ -30,7 +30,7 @@ public static void main(String[] args) {
3030
drawing.add(p3);
3131

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

ch17/Mole.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

ch17/MoleHill.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

ch17/MovingPolygon.java

Lines changed: 0 additions & 56 deletions
This file was deleted.

ch17/RegularPolygon.java

Lines changed: 4 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66
*/
77
public class RegularPolygon extends DrawablePolygon {
88

9-
public static final String[] NAMES = {null, null, null,
10-
"Triangle", "Square", "Pentagon", "Hexagon",
11-
"Heptagon", "Octagon", "Nonagon", "Decagon"};
12-
139
/**
1410
* Constructs a regular polygon, given the number of sides.
1511
*
@@ -26,7 +22,7 @@ public RegularPolygon(int nsides) {
2622
* @param radius from center to vertex
2723
*/
2824
public RegularPolygon(int nsides, int radius) {
29-
this(nsides, radius, Color.BLACK);
25+
this(nsides, radius, Color.GRAY);
3026
}
3127

3228
/**
@@ -57,43 +53,18 @@ public RegularPolygon(int nsides, int radius, Color color) {
5753
this.color = color;
5854

5955
// the amount to rotate for each vertex (in radians)
60-
double angle = 2.0 * Math.PI / nsides;
56+
double theta = 2.0 * Math.PI / nsides;
6157

6258
// rotation that makes the polygon right-side up
6359
double rotate = Math.PI / nsides + Math.PI / 2.0;
6460

6561
// compute x and y coordinates, centered at the origin
6662
for (int i = 0; i < nsides; i++) {
67-
double x = radius * Math.cos(i * angle + rotate);
63+
double x = radius * Math.cos(i * theta + rotate);
64+
double y = radius * Math.sin(i * theta + rotate);
6865
xpoints[i] = (int) Math.round(x);
69-
double y = radius * Math.sin(i * angle + rotate);
7066
ypoints[i] = (int) Math.round(y);
7167
}
7268
}
7369

74-
@Override
75-
public String toString() {
76-
StringBuilder str = new StringBuilder();
77-
if (npoints < NAMES.length) {
78-
str.append(NAMES[npoints]);
79-
} else {
80-
str.append("Polygon");
81-
}
82-
// append the list of vertexes
83-
str.append('[');
84-
for (int i = 0; i < npoints; i++) {
85-
if (i > 0) {
86-
str.append(", ");
87-
}
88-
// append the next (x, y) point
89-
str.append('(');
90-
str.append(xpoints[i]);
91-
str.append(", ");
92-
str.append(ypoints[i]);
93-
str.append(')');
94-
}
95-
str.append(']');
96-
return str.toString();
97-
}
98-
9970
}

ch17/RotatingPolygon.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

0 commit comments

Comments
 (0)