Skip to content

Commit 40f4366

Browse files
committed
added RegularPolygon
1 parent e296160 commit 40f4366

4 files changed

Lines changed: 122 additions & 3 deletions

File tree

ch15/Actor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface Actor {
1111
void act();
1212

1313
/**
14-
* Paints the simulation element in the context.
14+
* Draws the simulation element in the context.
1515
*
1616
* @param g graphics context
1717
*/

ch15/RegularPolygon.java

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import java.awt.Graphics;
2+
import java.awt.Polygon;
3+
4+
/**
5+
* A polygon that is equiangular (all angles are equal in measure) and
6+
* equilateral (all sides have the same length).
7+
*/
8+
public class RegularPolygon extends Polygon implements Actor {
9+
10+
public static final String[] NAMES = {null, null, null,
11+
"Triangle", "Square", "Pentagon", "Hexagon",
12+
"Heptagon", "Octagon", "Nonagon", "Decagon"};
13+
14+
/**
15+
* Constructs a regular polygon.
16+
*
17+
* @param nsides the number of sides
18+
* @param length length of each side
19+
*/
20+
public RegularPolygon(int nsides, int length) {
21+
22+
// validate and store arguments
23+
if (nsides < 3 || nsides >= NAMES.length) {
24+
throw new IllegalArgumentException("invalid nsides");
25+
}
26+
if (length < 1) {
27+
throw new IllegalArgumentException("invalid length");
28+
}
29+
30+
// initialize Polygon attributes
31+
npoints = nsides;
32+
xpoints = new int[nsides];
33+
ypoints = new int[nsides];
34+
35+
// compute the radius, angle between vertexes, and rotation
36+
double radius = length / (2.0 * Math.sin(Math.PI / nsides));
37+
double angle = 2.0 * Math.PI / nsides;
38+
double rotate = Math.PI / nsides + Math.PI / 2.0;
39+
40+
// compute x and y coordinates, centered around the origin
41+
for (int i = 0; i < nsides; i++) {
42+
xpoints[i] = (int) Math.round(
43+
radius * Math.cos(i * angle + rotate));
44+
ypoints[i] = (int) Math.round(
45+
radius * Math.sin(i * angle + rotate));
46+
}
47+
}
48+
49+
@Override
50+
public String toString() {
51+
StringBuilder str = new StringBuilder();
52+
str.append(NAMES[npoints]);
53+
str.append('[');
54+
for (int i = 0; i < npoints; i++) {
55+
if (i > 0) {
56+
str.append(", ");
57+
}
58+
// append the next (x, y) point
59+
str.append('(');
60+
str.append(xpoints[i]);
61+
str.append(", ");
62+
str.append(ypoints[i]);
63+
str.append(')');
64+
}
65+
str.append(']');
66+
return str.toString();
67+
}
68+
69+
@Override
70+
public void act() {
71+
// nothing to do
72+
}
73+
74+
@Override
75+
public void draw(Graphics g) {
76+
System.out.println(this);
77+
g.drawPolygon(this);
78+
}
79+
80+
}

ch15/Sim1.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import javax.swing.JFrame;
2+
3+
/**
4+
* Example simulation of stationary objects.
5+
*/
6+
public class Sim1 {
7+
8+
/**
9+
* Test program that draws a few polygons.
10+
*
11+
* @param args command-line arguments
12+
*/
13+
public static void main(String[] args) {
14+
15+
// create some regular polygons
16+
RegularPolygon p3 = new RegularPolygon(3, 100);
17+
RegularPolygon p4 = new RegularPolygon(4, 100);
18+
RegularPolygon p5 = new RegularPolygon(5, 100);
19+
20+
// move them out of the corner
21+
p3.translate(100, 100);
22+
p4.translate(250, 250);
23+
p5.translate(400, 400);
24+
25+
// add polygons to a drawing
26+
Drawing drawing = new Drawing(800, 600);
27+
drawing.add(p3);
28+
drawing.add(p4);
29+
drawing.add(p5);
30+
31+
// set up the window frame
32+
JFrame frame = new JFrame("Drawing");
33+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
34+
frame.add(drawing);
35+
frame.pack();
36+
frame.setVisible(true);
37+
}
38+
39+
}

ch15/Main.java renamed to ch15/Sim2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import javax.swing.JFrame;
22

33
/**
4-
* Example simulation of moving objects.
4+
* Example simulation of rotating objects.
55
*/
6-
public class Main {
6+
public class Sim2 {
77

88
/**
99
* Sets up the drawing, creates the actors, and runs the simulation.

0 commit comments

Comments
 (0)