-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDisplay.java
102 lines (84 loc) · 2.21 KB
/
Display.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
public class Display extends Canvas implements KeyListener, Runnable {
private static final long serialVersionUID = 1L;
private int width;
private int height;
int zoom;
private Automaton automaton;
private BufferedImage img;
private boolean k;
public Display(int width, int height, int zoom) {
this.width = width / zoom;
this.height = height / zoom;
this.zoom = zoom;
this.img = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB);
this.automaton = new Automaton(this.width, this.height, ((DataBufferInt) img.getRaster().getDataBuffer()).getData());
this.k = false;
}
private Thread thread;
private boolean running;
public void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
/*
* private void stop() { if (!running) return; running = false; try {
* thread.join(); } catch (InterruptedException e) { e.printStackTrace();
* System.exit(1); } }
*/
public void run() {
long elapsedTime = 0;
int frames = 0;
while (running) {
long time = System.nanoTime();
this.render();
frames++;
elapsedTime += (System.nanoTime() - time);
if (elapsedTime >= 1000000000) {
System.out.println(frames + " fps");
frames = 0;
elapsedTime = 0;
}
}
}
/*public void run() {
while(running) {
render();
}
}*/
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(2);
return;
}
if (k)
automaton.nextIteration();
Graphics gfx = bs.getDrawGraphics();
gfx.drawImage(img, 0, 0, zoom * width, zoom * height, null);
gfx.dispose();
bs.show();
}
@Override
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'k')
this.k = !this.k;
else if (e.getKeyChar() == 'r')
this.automaton = new Automaton(this.width, this.height, ((DataBufferInt) img.getRaster().getDataBuffer()).getData());
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
}