-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAlgoVisualizer.java
63 lines (47 loc) · 1.69 KB
/
AlgoVisualizer.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
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
public class AlgoVisualizer {
private static int DELAY = 10;
private InsertionSortData data; // 数据
private AlgoFrame frame; // 视图
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N, InsertionSortData.Type dataType) {
// 初始化数据
data = new InsertionSortData(N, sceneHeight, dataType);
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Welcome", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N) {
this(sceneWidth, sceneHeight, N, InsertionSortData.Type.Default);
}
// 动画逻辑
private void run() {
setData(0, -1);
for (int i = 0; i < data.N(); i++) {
setData(i, i);
for (int j = i; j > 0 && data.get(j) < data.get(j - 1); j--) {
data.swap(j, j - 1);
setData(i + 1, j - 1);
}
}
setData(data.N(), -1);
}
private void setData(int orderedIndex, int currentIndex) {
data.orderedIndex = orderedIndex;
data.currentIndex = currentIndex;
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
public static void main(String[] args) {
int sceneWidth = 800;
int sceneHeight = 800;
int N = 100;
// TODO: 根据需要设置其他参数,初始化visualizer
AlgoVisualizer visualizer = new AlgoVisualizer(sceneWidth, sceneHeight, N, InsertionSortData.Type.NearlyOrdered);
}
}