Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 33b1e75

Browse filesBrowse files
committed
ch16 revisions
1 parent be6bb54 commit 33b1e75
Copy full SHA for 33b1e75

File tree

Expand file treeCollapse file tree

2 files changed

+49
-19
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+49
-19
lines changed
Open diff view settings
Collapse file

‎ch16/Automaton.java‎

Copy file name to clipboardExpand all lines: ch16/Automaton.java
+17-17Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@ public abstract class Automaton {
1313
*/
1414
public abstract void update();
1515

16-
/**
17-
* Creates a JFrame and runs the automaton.
18-
*
19-
* @param title the frame title
20-
* @param rate frames per second
21-
*/
22-
public void run(String title, int rate) {
23-
// set up the window frame
24-
JFrame frame = new JFrame(title);
25-
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26-
frame.setResizable(false);
27-
frame.add(this.grid);
28-
frame.pack();
29-
frame.setVisible(true);
30-
this.mainloop(rate);
31-
}
32-
3316
/**
3417
* The simulation loop.
3518
*
@@ -51,4 +34,21 @@ private void mainloop(int rate) {
5134
}
5235
}
5336

37+
/**
38+
* Creates a JFrame and runs the automaton.
39+
*
40+
* @param title the frame title
41+
* @param rate frames per second
42+
*/
43+
public void run(String title, int rate) {
44+
// set up the window frame
45+
JFrame frame = new JFrame(title);
46+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
47+
frame.setResizable(false);
48+
frame.add(this.grid);
49+
frame.pack();
50+
frame.setVisible(true);
51+
this.mainloop(rate);
52+
}
53+
5454
}
Collapse file

‎ch16/Langton.java‎

Copy file name to clipboardExpand all lines: ch16/Langton.java
+32-2Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import javax.swing.JFrame;
2+
13
/**
24
* Langton's Ant.
35
*/
4-
public class Langton extends Automaton {
6+
public class Langton {
57

8+
private GridCanvas grid;
69
private int xpos;
710
private int ypos;
811
private int head; // 0=North, 1=East, 2=South, 3=West
@@ -59,6 +62,27 @@ public void update() {
5962
moveAnt();
6063
}
6164

65+
/**
66+
* The simulation loop.
67+
*
68+
* @param rate frames per second
69+
*/
70+
private void mainloop() {
71+
while (true) {
72+
73+
// update the drawing
74+
this.update();
75+
grid.repaint();
76+
77+
// delay the simulation
78+
try {
79+
Thread.sleep(2);
80+
} catch (InterruptedException e) {
81+
// do nothing
82+
}
83+
}
84+
}
85+
6286
/**
6387
* Creates and runs the simulation.
6488
*
@@ -67,7 +91,13 @@ public void update() {
6791
public static void main(String[] args) {
6892
String title = "Langton's Ant";
6993
Langton game = new Langton(61, 61);
70-
game.run(title, 750);
94+
JFrame frame = new JFrame(title);
95+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
96+
frame.setResizable(false);
97+
frame.add(game.grid);
98+
frame.pack();
99+
frame.setVisible(true);
100+
game.mainloop();
71101
}
72102

73103
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.