forked from tonikolaba/BatBat-Game
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGamePanel.java
162 lines (130 loc) · 4.06 KB
/
GamePanel.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package al.artofsoul.batbatgame.main;
import al.artofsoul.batbatgame.gamestate.GameStateManager;
import al.artofsoul.batbatgame.handlers.Keys;
import al.artofsoul.batbatgame.handlers.LoggingHelper;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
public class GamePanel extends JPanel implements Runnable, KeyListener {
// dimensions
public static final int WIDTH = 320;
public static final int HEIGHT = 240;
public static final int SCALE = 2;
/**
*
*/
private static final long serialVersionUID = 1275876853084636658L;
// game thread
private transient Thread thread;
private boolean running;
private int fps = 60;
private long targetTime = 1000 / fps;
// image
private transient BufferedImage image;
private transient Graphics2D g;
// game state manager
private transient GameStateManager gsm;
// other
private boolean recording = false;
private int recordingCount = 0;
private boolean screenshot;
public GamePanel() {
super();
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setFocusable(true);
requestFocus();
}
@Override
public void addNotify() {
super.addNotify();
if (thread == null) {
thread = new Thread(this);
addKeyListener(this);
thread.start();
}
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) image.getGraphics();
running = true;
gsm = new GameStateManager();
}
@Override
public void run() {
init();
long start;
long elapsed;
long wait;
// game loop
while (running) {
start = System.nanoTime();
update();
draw();
drawToScreen();
elapsed = System.nanoTime() - start;
wait = targetTime - elapsed / 1000000;
if (wait < 0)
wait = 5;
try {
Thread.sleep(wait);
} catch (Exception e) {
LoggingHelper.LOGGER.log(Level.SEVERE, e.getMessage());
}
}
}
private void update() {
gsm.update();
Keys.update();
}
private void draw() {
gsm.draw(g);
}
private void drawToScreen() {
Graphics g2 = getGraphics();
g2.drawImage(image, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g2.dispose();
if (screenshot) {
screenshot = false;
try {
java.io.File out = new java.io.File("screenshot " + System.nanoTime() + ".gif");
javax.imageio.ImageIO.write(image, "gif", out);
} catch (Exception e) {
LoggingHelper.LOGGER.log(Level.SEVERE, e.getMessage());
}
}
if (!recording)
return;
try {
java.io.File out = new java.io.File("C:\\out\\frame" + recordingCount + ".gif");
javax.imageio.ImageIO.write(image, "gif", out);
recordingCount++;
} catch (Exception e) {
LoggingHelper.LOGGER.log(Level.SEVERE, e.getMessage());
}
}
@Override
public void keyPressed(KeyEvent key) {
if (key.isControlDown()) {
if (key.getKeyCode() == KeyEvent.VK_R) {
recording = !recording;
return;
}
if (key.getKeyCode() == KeyEvent.VK_S) {
screenshot = true;
return;
}
}
Keys.keySet(key.getKeyCode(), true);
}
@Override
public void keyReleased(KeyEvent key) {
Keys.keySet(key.getKeyCode(), false);
}
@Override
public void keyTyped(KeyEvent e) {
// Not necessary
}
}