forked from liuyubobobo/Play-with-Algorithm-Visualization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlgoFrame.java
87 lines (64 loc) · 2.34 KB
/
AlgoFrame.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.awt.*;
import javax.swing.*;
public class AlgoFrame extends JFrame{
private int canvasWidth;
private int canvasHeight;
public AlgoFrame(String title, int canvasWidth, int canvasHeight){
super(title);
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
AlgoCanvas canvas = new AlgoCanvas();
setContentPane(canvas);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
setVisible(true);
}
public AlgoFrame(String title){
this(title, 1024, 768);
}
public int getCanvasWidth(){return canvasWidth;}
public int getCanvasHeight(){return canvasHeight;}
// data
private MazeData data;
public void render(MazeData data){
this.data = data;
repaint();
}
private class AlgoCanvas extends JPanel{
public AlgoCanvas(){
// 双缓存
super(true);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
// 抗锯齿
// RenderingHints hints = new RenderingHints(
// RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON);
// hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// g2d.addRenderingHints(hints);
// 具体绘制
int w = canvasWidth/data.M();
int h = canvasHeight/data.N();
for(int i = 0 ; i < data.N() ; i ++ )
for(int j = 0 ; j < data.M() ; j ++){
if (data.getMaze(i,j) == MazeData.WALL)
AlgoVisHelper.setColor(g2d, AlgoVisHelper.LightBlue);
else
AlgoVisHelper.setColor(g2d, AlgoVisHelper.White);
if(data.path[i][j])
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Yellow);
if(data.result[i][j])
AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
AlgoVisHelper.fillRectangle(g2d, j * w, i * h, w, h);
}
}
@Override
public Dimension getPreferredSize(){
return new Dimension(canvasWidth, canvasHeight);
}
}
}