-
Notifications
You must be signed in to change notification settings - Fork 173
/
AlgoVisualizer.java
49 lines (37 loc) · 1.13 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
import java.awt.*;
public class AlgoVisualizer {
private static int DELAY = 10;
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){
frame.render(money);
AlgoVisHelper.pause(DELAY);
for(int i = 0 ; i < money.length; i ++){
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);
}
}