Skip to content

Commit 0ed4dad

Browse files
committed
Chapter 07 section 01-03 codes updated.
1 parent 9465ab4 commit 0ed4dad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+676
-148
lines changed

07-Mine-Sweeper/01-Mine-Sweeper-Basics/src/AlgoFrame.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
import java.awt.Graphics2D;
2-
import java.awt.Graphics;
3-
import java.awt.Dimension;
4-
import java.awt.Color;
5-
import java.awt.RenderingHints;
6-
1+
import java.awt.*;
72
import javax.swing.*;
83

94
public class AlgoFrame extends JFrame{
105

116
private int canvasWidth;
127
private int canvasHeight;
13-
private JPanel canvas;
148

159
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
1610

@@ -38,8 +32,8 @@ public AlgoFrame(String title){
3832
public int getCanvasHeight(){return canvasHeight;}
3933

4034
// data
41-
MineSweeperData data;
42-
public void setData(MineSweeperData data){
35+
private MineSweeperData data;
36+
public void render(MineSweeperData data){
4337
this.data = data;
4438
repaint();
4539
}
@@ -58,11 +52,11 @@ public void paintComponent(Graphics g) {
5852
Graphics2D g2d = (Graphics2D)g;
5953

6054
// 抗锯齿
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);
55+
RenderingHints hints = new RenderingHints(
56+
RenderingHints.KEY_ANTIALIASING,
57+
RenderingHints.VALUE_ANTIALIAS_ON);
58+
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
59+
g2d.addRenderingHints(hints);
6660

6761
// 具体绘制
6862
int w = canvasWidth/data.M();

07-Mine-Sweeper/01-Mine-Sweeper-Basics/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: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,46 @@
11
import java.awt.*;
2-
import java.util.LinkedList;
3-
import java.util.Random;
4-
import java.util.Arrays;
5-
import java.util.Queue;
62

73
public class AlgoVisualizer {
84

95
private static int DELAY = 5;
6+
private static int blockSide = 32;
107

118
private MineSweeperData data;
129
private AlgoFrame frame;
13-
private final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
10+
private static final int d[][] = {{-1,0},{0,1},{1,0},{0,-1}};
1411

15-
public AlgoVisualizer(AlgoFrame frame, MineSweeperData data){
12+
public AlgoVisualizer(int N, int M, int mineNumber){
1613

17-
this.frame = frame;
18-
this.data = data;
14+
data = new MineSweeperData(N, M, mineNumber);
15+
int sceneWidth = M * blockSide;
16+
int sceneHeight = N * blockSide;
1917

20-
this.setData();
18+
EventQueue.invokeLater(() -> {
19+
frame = new AlgoFrame("Mine Sweeper", sceneWidth,sceneHeight);
20+
21+
new Thread(() -> {
22+
run();
23+
}).start();
24+
});
2125
}
2226

23-
public void run(){
27+
private void run(){
2428

25-
this.setData();
26-
AlgoVisHelper.pause(DELAY);
29+
setData();
2730
}
2831

2932
private void setData(){
30-
frame.setData(data);
33+
frame.render(data);
34+
AlgoVisHelper.pause(DELAY);
3135
}
3236

3337
public static void main(String[] args) {
3438

3539
int N = 20;
3640
int M = 20;
37-
38-
int blockSide = 32;
39-
int sceneWidth = M * blockSide;
40-
int sceneHeight = N * blockSide;
41-
4241
int mineNumber = 1;
43-
MineSweeperData data = new MineSweeperData(N, M, mineNumber);
4442

45-
EventQueue.invokeLater(() -> {
46-
AlgoFrame frame = new AlgoFrame("Mine Sweeper", sceneWidth,sceneHeight);
43+
AlgoVisualizer vis = new AlgoVisualizer(N, M, mineNumber);
4744

48-
AlgoVisualizer vis = new AlgoVisualizer(frame, data);
49-
new Thread(() -> {
50-
vis.run();
51-
}).start();
52-
});
5345
}
5446
}

07-Mine-Sweeper/01-Mine-Sweeper-Basics/src/MineSweeperData.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
public class MineSweeperData {
22

3-
public static String blockImageURL = "resources/block.png";
4-
public static String flagImageURL = "resources/flag.png";
5-
public static String mineImageURL = "resources/mine.png";
3+
public static final String blockImageURL = "resources/block.png";
4+
public static final String flagImageURL = "resources/flag.png";
5+
public static final String mineImageURL = "resources/mine.png";
66
public static String numberImageURL(int num){
77
if(num < 0 || num >= 8)
88
throw new IllegalArgumentException("No such a number image!");
@@ -17,7 +17,7 @@ public MineSweeperData(int N, int M, int mineNumber){
1717
if(N <= 0 || M <= 0)
1818
throw new IllegalArgumentException("Mine sweeper size is invalid!");
1919

20-
if(mineNumber > N*M)
20+
if(mineNumber < 0 || mineNumber > N*M)
2121
throw new IllegalArgumentException("Mine number is larger than the size of mine sweeper board!");
2222

2323
this.N = N;

0 commit comments

Comments
 (0)