Skip to content

Commit d3e1ad2

Browse files
committed
Chapter 08 codes updated.
1 parent f111ef0 commit d3e1ad2

40 files changed

+488
-691
lines changed

08-Move-the-Box-Solver/02-Move-the-Box-Data/src/AlgoFrame.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ public class AlgoFrame extends JFrame{
1010

1111
private int canvasWidth;
1212
private int canvasHeight;
13-
private JPanel canvas;
1413

1514
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1615

@@ -38,8 +37,8 @@ public AlgoFrame(String title){
3837
public int getCanvasHeight(){return canvasHeight;}
3938

4039
// data
41-
GameData data;
42-
public void setData(GameData data){
40+
private GameData data;
41+
public void render(GameData data){
4342
this.data = data;
4443
repaint();
4544
}
@@ -58,11 +57,11 @@ public void paintComponent(Graphics g) {
5857
Graphics2D g2d = (Graphics2D)g;
5958

6059
// 抗锯齿
61-
// RenderingHints hints = new RenderingHints(
62-
// RenderingHints.KEY_ANTIALIASING,
63-
// RenderingHints.VALUE_ANTIALIAS_ON);
64-
// hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
65-
// g2d.addRenderingHints(hints);
60+
RenderingHints hints = new RenderingHints(
61+
RenderingHints.KEY_ANTIALIASING,
62+
RenderingHints.VALUE_ANTIALIAS_ON);
63+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
64+
g2d.addRenderingHints(hints);
6665

6766
// 具体绘制
6867
int w = canvasWidth/data.M();

08-Move-the-Box-Solver/02-Move-the-Box-Data/src/AlgoVisHelper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import javax.swing.*;
22
import java.awt.*;
33
import java.awt.geom.Ellipse2D;
4-
54
import java.awt.geom.Rectangle2D;
65
import java.lang.InterruptedException;
76

8-
97
public class AlgoVisHelper {
108

119
private AlgoVisHelper(){}
@@ -33,35 +31,35 @@ private AlgoVisHelper(){}
3331
public static final Color White = new Color(0xFFFFFF);
3432

3533

36-
static public void strokeCircle(Graphics2D g, int x, int y, int r){
34+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
3735

3836
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
3937
g.draw(circle);
4038
}
4139

42-
static public void fillCircle(Graphics2D g, int x, int y, int r){
40+
public static void fillCircle(Graphics2D g, int x, int y, int r){
4341

4442
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4543
g.fill(circle);
4644
}
4745

48-
static public void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
46+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
4947

5048
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5149
g.draw(rectangle);
5250
}
5351

54-
static public void fillRectangle(Graphics2D g, int x, int y, int w, int h){
52+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5553

5654
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5755
g.fill(rectangle);
5856
}
5957

60-
static public void setColor(Graphics2D g, Color color){
58+
public static void setColor(Graphics2D g, Color color){
6159
g.setColor(color);
6260
}
6361

64-
static public void setStrokeWidth(Graphics2D g, int w){
62+
public static void setStrokeWidth(Graphics2D g, int w){
6563
int strokeWidth = w;
6664
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6765
}
Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,43 @@
1-
import javafx.scene.input.MouseButton;
2-
31
import java.awt.*;
42
import java.awt.event.*;
5-
import javax.swing.*;
63

74
public class AlgoVisualizer {
85

96
private static int DELAY = 5;
7+
private static int blockSide = 80;
108

119
private GameData data;
1210
private AlgoFrame frame;
13-
private final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
14-
15-
public AlgoVisualizer(AlgoFrame frame, GameData data){
1611

17-
this.frame = frame;
18-
this.data = data;
19-
20-
this.setData();
21-
}
12+
public AlgoVisualizer(String filename){
2213

23-
public void run(){
14+
data = new GameData(filename);
15+
int sceneWidth = data.M() * blockSide;
16+
int sceneHeight = data.N() * blockSide;
2417

25-
this.setData();
26-
AlgoVisHelper.pause(DELAY);
27-
}
18+
EventQueue.invokeLater(() -> {
19+
frame = new AlgoFrame("Move the Box Solver", sceneWidth,sceneHeight);
2820

29-
public void addAlgoMouseListener(){
30-
frame.addMouseListener(new AlgoMouseListener());
21+
new Thread(() -> {
22+
run();
23+
}).start();
24+
});
3125
}
3226

33-
private class AlgoMouseListener extends MouseAdapter{
34-
35-
@Override
36-
public void mouseReleased(MouseEvent event){
37-
event.translatePoint(
38-
-(int)(frame.getBounds().width - frame.getCanvasWidth()),
39-
-(int)(frame.getBounds().height - frame.getCanvasHeight())
40-
);
41-
42-
Point pos = event.getPoint();
43-
44-
int w = frame.getCanvasWidth() / data.M();
45-
int h = frame.getCanvasHeight() / data.N();
46-
47-
int x = pos.x / h;
48-
int y = pos.y / w;
49-
// System.out.println(x + " , " + y);
27+
public void run(){
5028

51-
}
29+
setData();
5230
}
5331

5432
private void setData(){
55-
frame.setData(data);
33+
frame.render(data);
34+
AlgoVisHelper.pause(DELAY);
5635
}
5736

5837
public static void main(String[] args) {
5938

6039
String filename = "level/boston_09.txt";
61-
GameData data = new GameData(filename);
6240

63-
int blockSide = 80;
64-
int sceneWidth = data.M() * blockSide;
65-
int sceneHeight = data.N() * blockSide;
66-
67-
EventQueue.invokeLater(() -> {
68-
AlgoFrame frame = new AlgoFrame("Move the Box Solver", sceneWidth,sceneHeight);
69-
70-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
71-
vis.addAlgoMouseListener();
72-
new Thread(() -> {
73-
vis.run();
74-
}).start();
75-
});
41+
AlgoVisualizer vis = new AlgoVisualizer(filename);
7642
}
7743
}

08-Move-the-Box-Solver/03-Move-the-Box-Solver-Rendering/src/AlgoFrame.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
import java.awt.Dimension;
44
import java.awt.Color;
55
import java.awt.RenderingHints;
6-
import java.util.ArrayList;
7-
import java.util.Map;
8-
import java.util.HashMap;
9-
6+
import java.util.*;
107
import javax.swing.*;
118

129
public class AlgoFrame extends JFrame{
1310

1411
private int canvasWidth;
1512
private int canvasHeight;
16-
private JPanel canvas;
1713

1814
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1915

@@ -41,16 +37,16 @@ public AlgoFrame(String title){
4137
public int getCanvasHeight(){return canvasHeight;}
4238

4339
// data
44-
GameData data;
45-
public void setData(GameData data){
40+
private GameData data;
41+
public void render(GameData data){
4642
this.data = data;
4743
repaint();
4844
}
4945

5046
private class AlgoCanvas extends JPanel{
5147

5248
private ArrayList<Color> colorList;
53-
private Map<Character, Color> colorMap;
49+
private HashMap<Character, Color> colorMap;
5450
public AlgoCanvas(){
5551
// 双缓存
5652
super(true);
@@ -77,11 +73,11 @@ public void paintComponent(Graphics g) {
7773
Graphics2D g2d = (Graphics2D)g;
7874

7975
// 抗锯齿
80-
// RenderingHints hints = new RenderingHints(
81-
// RenderingHints.KEY_ANTIALIASING,
82-
// RenderingHints.VALUE_ANTIALIAS_ON);
83-
// hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
84-
// g2d.addRenderingHints(hints);
76+
RenderingHints hints = new RenderingHints(
77+
RenderingHints.KEY_ANTIALIASING,
78+
RenderingHints.VALUE_ANTIALIAS_ON);
79+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
80+
g2d.addRenderingHints(hints);
8581

8682
// 具体绘制
8783
int w = canvasWidth/data.M();

08-Move-the-Box-Solver/03-Move-the-Box-Solver-Rendering/src/AlgoVisHelper.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import javax.swing.*;
22
import java.awt.*;
33
import java.awt.geom.Ellipse2D;
4-
54
import java.awt.geom.Rectangle2D;
65
import java.lang.InterruptedException;
76

8-
97
public class AlgoVisHelper {
108

119
private AlgoVisHelper(){}
@@ -33,35 +31,35 @@ private AlgoVisHelper(){}
3331
public static final Color White = new Color(0xFFFFFF);
3432

3533

36-
static public void strokeCircle(Graphics2D g, int x, int y, int r){
34+
public static void strokeCircle(Graphics2D g, int x, int y, int r){
3735

3836
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
3937
g.draw(circle);
4038
}
4139

42-
static public void fillCircle(Graphics2D g, int x, int y, int r){
40+
public static void fillCircle(Graphics2D g, int x, int y, int r){
4341

4442
Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
4543
g.fill(circle);
4644
}
4745

48-
static public void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
46+
public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
4947

5048
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5149
g.draw(rectangle);
5250
}
5351

54-
static public void fillRectangle(Graphics2D g, int x, int y, int w, int h){
52+
public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
5553

5654
Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
5755
g.fill(rectangle);
5856
}
5957

60-
static public void setColor(Graphics2D g, Color color){
58+
public static void setColor(Graphics2D g, Color color){
6159
g.setColor(color);
6260
}
6361

64-
static public void setStrokeWidth(Graphics2D g, int w){
62+
public static void setStrokeWidth(Graphics2D g, int w){
6563
int strokeWidth = w;
6664
g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
6765
}
Lines changed: 16 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,43 @@
1-
import javafx.scene.input.MouseButton;
2-
31
import java.awt.*;
42
import java.awt.event.*;
5-
import javax.swing.*;
63

74
public class AlgoVisualizer {
85

96
private static int DELAY = 5;
7+
private static int blockSide = 80;
108

119
private GameData data;
1210
private AlgoFrame frame;
13-
private final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
14-
15-
public AlgoVisualizer(AlgoFrame frame, GameData data){
1611

17-
this.frame = frame;
18-
this.data = data;
19-
20-
this.setData();
21-
}
12+
public AlgoVisualizer(String filename){
2213

23-
public void run(){
14+
data = new GameData(filename);
15+
int sceneWidth = data.M() * blockSide;
16+
int sceneHeight = data.N() * blockSide;
2417

25-
this.setData();
26-
AlgoVisHelper.pause(DELAY);
27-
}
18+
EventQueue.invokeLater(() -> {
19+
frame = new AlgoFrame("Move the Box Solver", sceneWidth,sceneHeight);
2820

29-
public void addAlgoMouseListener(){
30-
frame.addMouseListener(new AlgoMouseListener());
21+
new Thread(() -> {
22+
run();
23+
}).start();
24+
});
3125
}
3226

33-
private class AlgoMouseListener extends MouseAdapter{
34-
35-
@Override
36-
public void mouseReleased(MouseEvent event){
37-
event.translatePoint(
38-
-(int)(frame.getBounds().width - frame.getCanvasWidth()),
39-
-(int)(frame.getBounds().height - frame.getCanvasHeight())
40-
);
41-
42-
Point pos = event.getPoint();
43-
44-
int w = frame.getCanvasWidth() / data.M();
45-
int h = frame.getCanvasHeight() / data.N();
46-
47-
int x = pos.x / h;
48-
int y = pos.y / w;
49-
// System.out.println(x + " , " + y);
27+
public void run(){
5028

51-
}
29+
setData();
5230
}
5331

5432
private void setData(){
55-
frame.setData(data);
33+
frame.render(data);
34+
AlgoVisHelper.pause(DELAY);
5635
}
5736

5837
public static void main(String[] args) {
5938

6039
String filename = "level/boston_09.txt";
61-
GameData data = new GameData(filename);
6240

63-
int blockSide = 80;
64-
int sceneWidth = data.M() * blockSide;
65-
int sceneHeight = data.N() * blockSide;
66-
67-
EventQueue.invokeLater(() -> {
68-
AlgoFrame frame = new AlgoFrame("Move the Box Solver", sceneWidth,sceneHeight);
69-
70-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
71-
vis.addAlgoMouseListener();
72-
new Thread(() -> {
73-
vis.run();
74-
}).start();
75-
});
41+
AlgoVisualizer vis = new AlgoVisualizer(filename);
7642
}
7743
}

08-Move-the-Box-Solver/03-Move-the-Box-Solver-Rendering/src/Board.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
public class Board {
32

43
public static char EMPTY = '.';

0 commit comments

Comments
 (0)