Skip to content

Commit bdafc13

Browse files
committed
RegularPolygon as base
1 parent 0059f5e commit bdafc13

5 files changed

Lines changed: 94 additions & 31 deletions

File tree

‎ch15/RegularPolygon.java‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,73 @@
1+
import java.awt.Color;
12
import java.awt.Graphics;
23
import java.awt.Polygon;
34

45
/**
56
* A polygon that is equiangular (all angles are equal in measure) and
6-
* equilateral (all sides have the same length).
7+
* equilateral (all sides have the same length). It also has a color.
78
*/
89
public class RegularPolygon extends Polygon implements Actor {
910

1011
public static final String[] NAMES = {null, null, null,
1112
"Triangle", "Square", "Pentagon", "Hexagon",
1213
"Heptagon", "Octagon", "Nonagon", "Decagon"};
1314

15+
private Color color;
16+
1417
/**
15-
* Constructs a regular polygon.
18+
* Constructs a regular polygon, given the number of sides.
19+
*
20+
* @param nsides the number of sides
21+
*/
22+
public RegularPolygon(int nsides) {
23+
this(nsides, 100);
24+
}
25+
26+
/**
27+
* Constructs a regular polygon, given the number of sides and length of
28+
* each side.
1629
*
1730
* @param nsides the number of sides
1831
* @param length length of each side
1932
*/
2033
public RegularPolygon(int nsides, int length) {
34+
this(nsides, length, Color.BLACK);
35+
}
36+
37+
/**
38+
* Constructs a regular polygon, given the number of sides, the length of
39+
* each side, and fill color.
40+
*
41+
* @param nsides the number of sides
42+
* @param length length of each side
43+
* @param color the fill color
44+
*/
45+
public RegularPolygon(int nsides, int length, Color color) {
2146

2247
// validate and store arguments
23-
if (nsides < 3 || nsides >= NAMES.length) {
48+
if (nsides < 3 || nsides >= 10) {
2449
throw new IllegalArgumentException("invalid nsides");
2550
}
2651
if (length < 1) {
2752
throw new IllegalArgumentException("invalid length");
2853
}
54+
if (color == null) {
55+
throw new NullPointerException("invalid color");
56+
}
2957

3058
// initialize Polygon attributes
31-
npoints = nsides;
32-
xpoints = new int[nsides];
33-
ypoints = new int[nsides];
59+
this.npoints = nsides;
60+
this.xpoints = new int[nsides];
61+
this.ypoints = new int[nsides];
62+
this.color = color;
3463

35-
// compute the radius, angle between vertexes, and rotation
64+
// radius of the polygon (distance from origin to vertex)
3665
double radius = length / (2.0 * Math.sin(Math.PI / nsides));
66+
67+
// the angle (in radians) at each vertex
3768
double angle = 2.0 * Math.PI / nsides;
69+
70+
// rotation that makes the polygon right-side up
3871
double rotate = Math.PI / nsides + Math.PI / 2.0;
3972

4073
// compute x and y coordinates, centered around the origin
@@ -74,7 +107,8 @@ public void act() {
74107
@Override
75108
public void draw(Graphics g) {
76109
// System.out.println(this);
77-
g.drawPolygon(this);
110+
g.setColor(color);
111+
g.fillPolygon(this);
78112
}
79113

80114
}

‎ch15/RotatingPolygon.java‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import java.awt.Graphics;
2-
import java.awt.Polygon;
31
import java.util.Arrays;
42

53
/**
6-
* A polygon that rotates each time act is called.
4+
* A polygon that rotates around its center.
75
*/
8-
public class RotatingPolygon extends Polygon implements Actor {
6+
public class RotatingPolygon extends RegularPolygon {
97

108
private int xmid;
119
private int ymid;
@@ -15,6 +13,17 @@ public class RotatingPolygon extends Polygon implements Actor {
1513
private int[] x0; // original xpoints
1614
private int[] y0; // original ypoints
1715

16+
/**
17+
* Constructs a moving polygon.
18+
*
19+
* @param nsides the number of sides
20+
* @param length length of each side
21+
*/
22+
public RotatingPolygon(int nsides, int length) {
23+
super(nsides, length);
24+
center();
25+
}
26+
1827
/**
1928
* Sets the rotation point to the middle.
2029
*/
@@ -40,9 +49,7 @@ public void center() {
4049
y0 = Arrays.copyOf(ypoints, npoints);
4150
}
4251

43-
/**
44-
* Rotates the polygon around its center.
45-
*/
52+
@Override
4653
public void act() {
4754
// update the rotation angle
4855
angle = (angle + 1) % 360;
@@ -59,13 +66,10 @@ public void act() {
5966
}
6067
}
6168

62-
/**
63-
* Paints the polygon on the screen.
64-
*
65-
* @param g graphics context
66-
*/
67-
public void draw(Graphics g) {
68-
g.fillPolygon(this);
69+
@Override
70+
public void translate(int deltaX, int deltaY) {
71+
super.translate(deltaX, deltaY);
72+
center();
6973
}
7074

7175
}

‎ch15/Sim1.java‎

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

34
/**
@@ -13,16 +14,16 @@ public class Sim1 {
1314
public static void main(String[] args) {
1415

1516
// 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);
17+
RegularPolygon p3 = new RegularPolygon(3, 100, Color.GREEN);
18+
RegularPolygon p4 = new RegularPolygon(4, 100, Color.ORANGE);
19+
RegularPolygon p5 = new RegularPolygon(5, 100, Color.MAGENTA);
1920

2021
// move them out of the corner
2122
p3.translate(100, 100);
2223
p4.translate(250, 250);
2324
p5.translate(400, 400);
2425

25-
// add polygons to a drawing
26+
// create drawing, add polygons
2627
Drawing drawing = new Drawing(800, 600);
2728
drawing.add(p3);
2829
drawing.add(p4);
@@ -31,6 +32,7 @@ public static void main(String[] args) {
3132
// set up the window frame
3233
JFrame frame = new JFrame("Drawing");
3334
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
35+
frame.setResizable(false);
3436
frame.add(drawing);
3537
frame.pack();
3638
frame.setVisible(true);

‎ch15/Sim3.java‎

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

33
/**
4-
* Example simulation of stationary objects.
4+
* Example simulation of moving objects.
55
*/
66
public class Sim3 {
77

@@ -12,11 +12,20 @@ public class Sim3 {
1212
*/
1313
public static void main(String[] args) {
1414

15+
// create some regular polygons
1516
MovingPolygon mp = new MovingPolygon(8, 30);
17+
RotatingPolygon rp = new RotatingPolygon(5, 50);
18+
19+
// move them out of the corner
1620
mp.translate(100, 100);
21+
rp.translate(200, 200);
22+
23+
// create drawing, add polygons
1724
Drawing drawing = new Drawing(800, 600);
1825
drawing.add(mp);
26+
drawing.add(rp);
1927

28+
// set up the window frame
2029
JFrame frame = new JFrame("Drawing");
2130
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2231
frame.setResizable(false);
@@ -33,7 +42,7 @@ public static void main(String[] args) {
3342

3443
// delay the simulation
3544
try {
36-
Thread.sleep(1000 / 50);
45+
Thread.sleep(1000 / 60);
3746
frame.getToolkit().sync();
3847
} catch (InterruptedException e) {
3948
// do nothing

‎ch15/Sim4.java‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22
import java.awt.event.ActionListener;
33

44
import javax.swing.JFrame;
5+
import javax.swing.Timer;
56

67
/**
7-
* Example simulation of stationary objects.
8+
* Smoother version using a timer.
89
*/
910
public class Sim4 implements ActionListener {
1011

1112
private Drawing drawing;
1213
private JFrame frame;
1314

15+
/**
16+
* Set up the drawing and window frame.
17+
*/
1418
public Sim4() {
19+
// create some regular polygons
1520
MovingPolygon mp = new MovingPolygon(8, 30);
21+
RotatingPolygon rp = new RotatingPolygon(5, 50);
22+
23+
// move them out of the corner
1624
mp.translate(100, 100);
25+
rp.translate(200, 200);
26+
27+
// create drawing, add polygons
1728
drawing = new Drawing(800, 600);
1829
drawing.add(mp);
30+
drawing.add(rp);
1931

32+
// set up the window frame
2033
frame = new JFrame("Drawing");
2134
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2235
frame.setResizable(false);
@@ -26,13 +39,14 @@ public Sim4() {
2639
}
2740

2841
/**
29-
* Test program that draws a few polygons.
42+
* Create and start the timer.
3043
*
3144
* @param args command-line arguments
3245
*/
3346
public static void main(String[] args) {
3447
Sim4 sim = new Sim4();
35-
new javax.swing.Timer(1000 / 50, sim).start();
48+
Timer timer = new Timer(1000 / 60, sim);
49+
timer.start();
3650
}
3751

3852
@Override

0 commit comments

Comments
 (0)