Skip to content

Commit 5f22f20

Browse files
committed
swap ch15 and ch16
1 parent c5d2c63 commit 5f22f20

17 files changed

Lines changed: 57 additions & 58 deletions

ch15/.classpath

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<classpath>
33
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
4-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
54
<classpathentry kind="src" path=""/>
65
<classpathentry kind="output" path=""/>
76
</classpath>
File renamed without changes.

ch15/Drawing.java

Lines changed: 18 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,43 @@
11
import java.awt.Canvas;
2-
import java.awt.Color;
32
import java.awt.Graphics;
4-
import java.util.ArrayList;
5-
import java.util.List;
63

74
/**
8-
* Draws a collection of actors.
5+
* Drawing of a grid.
96
*/
107
public class Drawing extends Canvas {
118

12-
private List<Actor> actors;
9+
private Grid grid;
1310

1411
/**
15-
* Constructs a drawing of given size.
12+
* Constructs a drawing for the grid.
1613
*
17-
* @param width the width in pixels
18-
* @param height the height in pixels
14+
* @param grid the initialized grid
1915
*/
20-
public Drawing(int width, int height) {
21-
setSize(width, height);
22-
setBackground(Color.WHITE);
23-
actors = new ArrayList<Actor>();
16+
public Drawing(Grid grid) {
17+
this.grid = grid;
18+
setSize(grid.width(), grid.height());
2419
}
2520

2621
/**
27-
* Adds a new actor to the drawing.
22+
* Draws the grid, cell by cell.
2823
*
29-
* @param actor the actor to add
30-
*/
31-
public void add(Actor actor) {
32-
actors.add(actor);
33-
}
34-
35-
/**
36-
* Gets current actors as an array.
37-
*
38-
* @return array of actor objects
39-
*/
40-
public Object[] getActors() {
41-
return actors.toArray();
42-
}
43-
44-
/**
45-
* Calls the act method of each actor.
24+
* @param g graphics context
4625
*/
47-
public void nextact() {
48-
for (Actor actor : actors) {
49-
actor.act();
50-
}
26+
@Override
27+
public void paint(Graphics g) {
28+
grid.draw(g);
5129
}
5230

5331
/**
54-
* Draws all the actors on the canvas.
32+
* Overriding this method helps the simulation run more smoothly. Normally
33+
* the Canvas is cleared before painting, but there is no need to clear it
34+
* since paint draws the entire grid.
5535
*
5636
* @param g graphics context
5737
*/
58-
public void paint(Graphics g) {
59-
for (Actor actor : actors) {
60-
actor.draw(g);
61-
}
38+
@Override
39+
public void update(Graphics g) {
40+
grid.draw(g);
6241
}
6342

6443
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

ch16/Drawing.java

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,64 @@
11
import java.awt.Canvas;
2+
import java.awt.Color;
23
import java.awt.Graphics;
4+
import java.util.ArrayList;
5+
import java.util.List;
36

47
/**
5-
* Drawing of a grid.
8+
* Draws a collection of actors.
69
*/
710
public class Drawing extends Canvas {
811

9-
private Grid grid;
12+
private List<Actor> actors;
1013

1114
/**
12-
* Constructs a drawing for the grid.
15+
* Constructs a drawing of given size.
1316
*
14-
* @param grid the initialized grid
17+
* @param width the width in pixels
18+
* @param height the height in pixels
1519
*/
16-
public Drawing(Grid grid) {
17-
this.grid = grid;
18-
setSize(grid.width(), grid.height());
20+
public Drawing(int width, int height) {
21+
setSize(width, height);
22+
setBackground(Color.WHITE);
23+
actors = new ArrayList<Actor>();
1924
}
2025

2126
/**
22-
* Draws the grid, cell by cell.
27+
* Adds a new actor to the drawing.
2328
*
24-
* @param g graphics context
29+
* @param actor the actor to add
2530
*/
26-
@Override
27-
public void paint(Graphics g) {
28-
grid.draw(g);
31+
public void add(Actor actor) {
32+
actors.add(actor);
33+
}
34+
35+
/**
36+
* Gets current actors as an array.
37+
*
38+
* @return array of actor objects
39+
*/
40+
public Object[] getActors() {
41+
return actors.toArray();
42+
}
43+
44+
/**
45+
* Calls the act method of each actor.
46+
*/
47+
public void nextact() {
48+
for (Actor actor : actors) {
49+
actor.act();
50+
}
2951
}
3052

3153
/**
32-
* Overriding this method helps the simulation run more smoothly. Normally
33-
* the Canvas is cleared before painting, but there is no need to clear it
34-
* since paint draws the entire grid.
54+
* Draws all the actors on the canvas.
3555
*
3656
* @param g graphics context
3757
*/
38-
@Override
39-
public void update(Graphics g) {
40-
grid.draw(g);
58+
public void paint(Graphics g) {
59+
for (Actor actor : actors) {
60+
actor.draw(g);
61+
}
4162
}
4263

4364
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)