Skip to content

Commit b80ecd8

Browse files
committed
rename to Sim2 and Sim3
1 parent bdafc13 commit b80ecd8

2 files changed

Lines changed: 54 additions & 54 deletions

File tree

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import java.awt.event.ActionEvent;
2-
import java.awt.event.ActionListener;
3-
41
import javax.swing.JFrame;
5-
import javax.swing.Timer;
62

73
/**
8-
* Smoother version using a timer.
4+
* Example simulation of moving objects.
95
*/
10-
public class Sim4 implements ActionListener {
11-
12-
private Drawing drawing;
13-
private JFrame frame;
6+
public class Sim2 {
147

158
/**
16-
* Set up the drawing and window frame.
9+
* Test program that draws a few polygons.
10+
*
11+
* @param args command-line arguments
1712
*/
18-
public Sim4() {
13+
public static void main(String[] args) {
14+
1915
// create some regular polygons
2016
MovingPolygon mp = new MovingPolygon(8, 30);
2117
RotatingPolygon rp = new RotatingPolygon(5, 50);
@@ -25,35 +21,33 @@ public Sim4() {
2521
rp.translate(200, 200);
2622

2723
// create drawing, add polygons
28-
drawing = new Drawing(800, 600);
24+
Drawing drawing = new Drawing(800, 600);
2925
drawing.add(mp);
3026
drawing.add(rp);
3127

3228
// set up the window frame
33-
frame = new JFrame("Drawing");
29+
JFrame frame = new JFrame("Drawing");
3430
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3531
frame.setResizable(false);
3632
frame.add(drawing);
3733
frame.pack();
3834
frame.setVisible(true);
39-
}
40-
41-
/**
42-
* Create and start the timer.
43-
*
44-
* @param args command-line arguments
45-
*/
46-
public static void main(String[] args) {
47-
Sim4 sim = new Sim4();
48-
Timer timer = new Timer(1000 / 60, sim);
49-
timer.start();
50-
}
5135

52-
@Override
53-
public void actionPerformed(ActionEvent e) {
54-
drawing.nextact();
55-
drawing.repaint();
56-
frame.getToolkit().sync();
36+
// main simulation loop
37+
while (true) {
38+
39+
// update the drawing
40+
drawing.nextact();
41+
drawing.repaint();
42+
43+
// delay the simulation
44+
try {
45+
Thread.sleep(1000 / 60);
46+
frame.getToolkit().sync();
47+
} catch (InterruptedException e) {
48+
// do nothing
49+
}
50+
}
5751
}
5852

5953
}

‎ch15/Sim3.java‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1+
import java.awt.event.ActionEvent;
2+
import java.awt.event.ActionListener;
3+
14
import javax.swing.JFrame;
5+
import javax.swing.Timer;
26

37
/**
4-
* Example simulation of moving objects.
8+
* Smoother version using a timer.
59
*/
6-
public class Sim3 {
10+
public class Sim3 implements ActionListener {
11+
12+
private Drawing drawing;
13+
private JFrame frame;
714

815
/**
9-
* Test program that draws a few polygons.
10-
*
11-
* @param args command-line arguments
16+
* Set up the drawing and window frame.
1217
*/
13-
public static void main(String[] args) {
14-
18+
public Sim3() {
1519
// create some regular polygons
1620
MovingPolygon mp = new MovingPolygon(8, 30);
1721
RotatingPolygon rp = new RotatingPolygon(5, 50);
@@ -21,33 +25,35 @@ public static void main(String[] args) {
2125
rp.translate(200, 200);
2226

2327
// create drawing, add polygons
24-
Drawing drawing = new Drawing(800, 600);
28+
drawing = new Drawing(800, 600);
2529
drawing.add(mp);
2630
drawing.add(rp);
2731

2832
// set up the window frame
29-
JFrame frame = new JFrame("Drawing");
33+
frame = new JFrame("Drawing");
3034
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
3135
frame.setResizable(false);
3236
frame.add(drawing);
3337
frame.pack();
3438
frame.setVisible(true);
39+
}
40+
41+
/**
42+
* Create and start the timer.
43+
*
44+
* @param args command-line arguments
45+
*/
46+
public static void main(String[] args) {
47+
Sim3 sim = new Sim3();
48+
Timer timer = new Timer(1000 / 60, sim);
49+
timer.start();
50+
}
3551

36-
// main simulation loop
37-
while (true) {
38-
39-
// update the drawing
40-
drawing.nextact();
41-
drawing.repaint();
42-
43-
// delay the simulation
44-
try {
45-
Thread.sleep(1000 / 60);
46-
frame.getToolkit().sync();
47-
} catch (InterruptedException e) {
48-
// do nothing
49-
}
50-
}
52+
@Override
53+
public void actionPerformed(ActionEvent e) {
54+
drawing.nextact();
55+
drawing.repaint();
56+
frame.getToolkit().sync();
5157
}
5258

5359
}

0 commit comments

Comments
 (0)