forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSim2.java
More file actions
57 lines (47 loc) · 1.43 KB
/
Sim2.java
File metadata and controls
57 lines (47 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.awt.Color;
import java.awt.Toolkit;
import javax.swing.JFrame;
/**
* Example simulation of animated objects.
*/
public class Sim2 {
/**
* Test program that draws a few polygons.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
// create some regular polygons
BlinkingPolygon bp = new BlinkingPolygon(3, 20, Color.BLUE);
MovingPolygon mp = new MovingPolygon(8, 30);
RotatingPolygon rp = new RotatingPolygon(5, 40);
// move them out of the corner
bp.translate(50, 50);
mp.translate(100, 100);
rp.translate(200, 200);
// create drawing, add polygons
Drawing drawing = new Drawing(800, 600);
drawing.add(bp);
drawing.add(mp);
drawing.add(rp);
// set up the window frame
JFrame frame = new JFrame("Drawing with Thread.sleep");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(drawing);
frame.pack();
frame.setVisible(true);
// main simulation loop
Toolkit toolkit = frame.getToolkit();
while (true) {
// update the drawing
toolkit.sync();
drawing.step();
// delay the simulation
try {
Thread.sleep(1000 / 30);
} catch (InterruptedException e) {
// do nothing
}
}
}
}