-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathAlgoVisualizer.java
44 lines (35 loc) · 1.16 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
import java.awt.*;
public class AlgoVisualizer {
private Circle[] circles; // 数据
private AlgoFrame frame; // 视图
public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
// 初始化数据
circles = new Circle[N];
int R = 50;
for(int i = 0 ; i < N ; i ++ ) {
int x = (int)(Math.random()*(sceneWidth-2*R)) + R;
int y = (int)(Math.random()*(sceneHeight-2*R)) + R;
int vx = (int)(Math.random()*11) - 5;
int vy = (int)(Math.random()*11) - 5;
circles[i] = new Circle(x, y, R, vx, vy);
}
// 初始化视图
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Welcome", sceneWidth, sceneHeight);
new Thread(() -> {
run();
}).start();
});
}
// 动画逻辑
private void run(){
while(true){
// 绘制数据
frame.render(circles);
AlgoVisHelper.pause(20);
// 更新数据
for(Circle circle: circles)
circle.move(0, 0, frame.getCanvasWidth(), frame.getCanvasHeight());
}
}
}