-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.java
More file actions
66 lines (55 loc) · 1.69 KB
/
Display.java
File metadata and controls
66 lines (55 loc) · 1.69 KB
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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
/**
* Handles the graphical output for the CHIP-8 emulator.
*/
public class Display extends JPanel {
private static final long serialVersionUID = -5606672764531745977L;
private static final int SCALE = 10;
private static final int FIL = 32;
private static final int COL = 64;
private short[] screen;
private final int[] decayBuffer = new int[64 * 32];
private static final int MAX_DECAY = 3;
/** Flag indicating that the screen needs to be redrawn. */
public boolean drawFlag;
public Display() {
setBackground(Color.BLACK);
}
/**
* Sets the screen memory buffer.
*
* @param screen The screen buffer to use for rendering.
*/
public void setScreen(short[] screen) {
this.screen = screen;
}
@Override
public Dimension getPreferredSize() {
return new Dimension(COL * SCALE, FIL * SCALE);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (screen == null)
return;
for (var i = 0; i < screen.length; i++) {
if ((screen[i] & 0xFF) == 1) {
decayBuffer[i] = MAX_DECAY;
} else if (decayBuffer[i] > 0) {
decayBuffer[i]--;
}
}
for (var y = 0; y < FIL; y++) {
for (var x = 0; x < COL; x++) {
var pos = x + (y * COL);
if (decayBuffer[pos] > 0) {
g.setColor(Color.WHITE);
g.fillRect(x * SCALE, y * SCALE, SCALE, SCALE);
}
}
}
}
}