|
| 1 | +import java.awt.Color; |
1 | 2 | import java.awt.Graphics; |
2 | 3 | import java.awt.Polygon; |
3 | 4 |
|
4 | 5 | /** |
5 | 6 | * 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. |
7 | 8 | */ |
8 | 9 | public class RegularPolygon extends Polygon implements Actor { |
9 | 10 |
|
10 | 11 | public static final String[] NAMES = {null, null, null, |
11 | 12 | "Triangle", "Square", "Pentagon", "Hexagon", |
12 | 13 | "Heptagon", "Octagon", "Nonagon", "Decagon"}; |
13 | 14 |
|
| 15 | + private Color color; |
| 16 | + |
14 | 17 | /** |
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. |
16 | 29 | * |
17 | 30 | * @param nsides the number of sides |
18 | 31 | * @param length length of each side |
19 | 32 | */ |
20 | 33 | 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) { |
21 | 46 |
|
22 | 47 | // validate and store arguments |
23 | | - if (nsides < 3 || nsides >= NAMES.length) { |
| 48 | + if (nsides < 3 || nsides >= 10) { |
24 | 49 | throw new IllegalArgumentException("invalid nsides"); |
25 | 50 | } |
26 | 51 | if (length < 1) { |
27 | 52 | throw new IllegalArgumentException("invalid length"); |
28 | 53 | } |
| 54 | + if (color == null) { |
| 55 | + throw new NullPointerException("invalid color"); |
| 56 | + } |
29 | 57 |
|
30 | 58 | // 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; |
34 | 63 |
|
35 | | - // compute the radius, angle between vertexes, and rotation |
| 64 | + // radius of the polygon (distance from origin to vertex) |
36 | 65 | double radius = length / (2.0 * Math.sin(Math.PI / nsides)); |
| 66 | + |
| 67 | + // the angle (in radians) at each vertex |
37 | 68 | double angle = 2.0 * Math.PI / nsides; |
| 69 | + |
| 70 | + // rotation that makes the polygon right-side up |
38 | 71 | double rotate = Math.PI / nsides + Math.PI / 2.0; |
39 | 72 |
|
40 | 73 | // compute x and y coordinates, centered around the origin |
@@ -74,7 +107,8 @@ public void act() { |
74 | 107 | @Override |
75 | 108 | public void draw(Graphics g) { |
76 | 109 | // System.out.println(this); |
77 | | - g.drawPolygon(this); |
| 110 | + g.setColor(color); |
| 111 | + g.fillPolygon(this); |
78 | 112 | } |
79 | 113 |
|
80 | 114 | } |
0 commit comments