Skip to content

Commit 1d320f6

Browse files
committed
minor tweaks
1 parent b80ecd8 commit 1d320f6

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

ch15/RegularPolygon.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public RegularPolygon(int nsides, int length) {
4545
public RegularPolygon(int nsides, int length, Color color) {
4646

4747
// validate and store arguments
48-
if (nsides < 3 || nsides >= 10) {
48+
if (nsides < 3) {
4949
throw new IllegalArgumentException("invalid nsides");
5050
}
5151
if (length < 1) {
@@ -61,8 +61,8 @@ public RegularPolygon(int nsides, int length, Color color) {
6161
this.ypoints = new int[nsides];
6262
this.color = color;
6363

64-
// radius of the polygon (distance from origin to vertex)
65-
double radius = length / (2.0 * Math.sin(Math.PI / nsides));
64+
// radius of the polygon (distance from center to vertex)
65+
double radius = 0.5 * length / Math.sin(Math.PI / nsides);
6666

6767
// the angle (in radians) at each vertex
6868
double angle = 2.0 * Math.PI / nsides;
@@ -82,7 +82,12 @@ public RegularPolygon(int nsides, int length, Color color) {
8282
@Override
8383
public String toString() {
8484
StringBuilder str = new StringBuilder();
85-
str.append(NAMES[npoints]);
85+
if (npoints < NAMES.length) {
86+
str.append(NAMES[npoints]);
87+
} else {
88+
str.append("Polygon");
89+
}
90+
// append the list of vertexes
8691
str.append('[');
8792
for (int i = 0; i < npoints; i++) {
8893
if (i > 0) {

ch15/Sim1.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void main(String[] args) {
1616
// create some regular polygons
1717
RegularPolygon p3 = new RegularPolygon(3, 100, Color.GREEN);
1818
RegularPolygon p4 = new RegularPolygon(4, 100, Color.ORANGE);
19-
RegularPolygon p5 = new RegularPolygon(5, 100, Color.MAGENTA);
19+
RegularPolygon p5 = new RegularPolygon(360, 1, Color.MAGENTA);
2020

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

ch15/Sim2.java

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

34
/**
@@ -34,6 +35,7 @@ public static void main(String[] args) {
3435
frame.setVisible(true);
3536

3637
// main simulation loop
38+
Toolkit toolkit = frame.getToolkit();
3739
while (true) {
3840

3941
// update the drawing
@@ -43,7 +45,7 @@ public static void main(String[] args) {
4345
// delay the simulation
4446
try {
4547
Thread.sleep(1000 / 60);
46-
frame.getToolkit().sync();
48+
toolkit.sync();
4749
} catch (InterruptedException e) {
4850
// do nothing
4951
}

ch15/Sim3.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import java.awt.Toolkit;
12
import java.awt.event.ActionEvent;
23
import java.awt.event.ActionListener;
3-
44
import javax.swing.JFrame;
55
import javax.swing.Timer;
66

@@ -10,7 +10,7 @@
1010
public class Sim3 implements ActionListener {
1111

1212
private Drawing drawing;
13-
private JFrame frame;
13+
private Toolkit toolkit;
1414

1515
/**
1616
* Set up the drawing and window frame.
@@ -30,12 +30,13 @@ public Sim3() {
3030
drawing.add(rp);
3131

3232
// set up the window frame
33-
frame = new JFrame("Drawing");
33+
JFrame frame = new JFrame("Drawing");
3434
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3535
frame.setResizable(false);
3636
frame.add(drawing);
3737
frame.pack();
3838
frame.setVisible(true);
39+
toolkit = frame.getToolkit();
3940
}
4041

4142
/**
@@ -53,7 +54,7 @@ public static void main(String[] args) {
5354
public void actionPerformed(ActionEvent e) {
5455
drawing.nextact();
5556
drawing.repaint();
56-
frame.getToolkit().sync();
57+
toolkit.sync();
5758
}
5859

5960
}

0 commit comments

Comments
 (0)