-
Notifications
You must be signed in to change notification settings - Fork 173
/
Copy pathAlgoVisualizer.java
58 lines (46 loc) · 1.46 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Arrays;
public class AlgoVisualizer {
private static int DELAY = 40;
private int[] money;
private AlgoFrame frame;
public AlgoVisualizer(int sceneWidth, int sceneHeight){
// 初始化数据
money = new int[100];
for(int i = 0 ; i < money.length ; i ++)
money[i] = 100;
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Money Problem", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
public void run(){
while(true){
// 改进2:是否排序
Arrays.sort(money);
frame.render(money);
AlgoVisHelper.pause(DELAY);
// 改进1:每一帧执行的轮数
for(int k = 0 ; k < 50 ; k ++){
for(int i = 0 ; i < money.length; i ++){
// 改进3:允许money为负值
//if(money[i] > 0){
int j = (int)(Math.random() * money.length);
money[i] -= 1;
money[j] += 1;
//}
}
}
}
}
public static void main(String[] args) {
int sceneWidth = 1000;
int sceneHeight = 800;
AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight);
}
}