-
Notifications
You must be signed in to change notification settings - Fork 173
/
AlgoVisualizer.java
60 lines (44 loc) · 1.39 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
import java.awt.*;
import java.awt.event.*;
public class AlgoVisualizer {
private static int DELAY = 40;
private FractalData data;
private AlgoFrame frame;
public AlgoVisualizer(int depth){
data = new FractalData(depth);
int sceneWidth = (int)Math.pow(3, depth);
int sceneHeight = (int)Math.pow(3, depth);
EventQueue.invokeLater(() -> {
frame = new AlgoFrame("Fractal Visualizer", sceneWidth,sceneHeight);
frame.addKeyListener(new AlgoKeyListener());
new Thread(() -> {
run();
}).start();
});
}
private void run(){
setData(data.depth);
}
private void setData(int depth){
data.depth = depth;
frame.render(data);
AlgoVisHelper.pause(DELAY);
}
public void addAlgoKeyListener(){
frame.addKeyListener(new AlgoKeyListener());
}
private class AlgoKeyListener extends KeyAdapter{
@Override
public void keyReleased(KeyEvent event){
//System.out.println("Key released:" + event);
if(event.getKeyChar() >= '0' && event.getKeyChar() <= '9'){
int depth = event.getKeyChar() - '0';
setData(depth);
}
}
}
public static void main(String[] args) {
int depth = 6;
AlgoVisualizer vis = new AlgoVisualizer(depth);
}
}