-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.java
More file actions
96 lines (81 loc) · 2.11 KB
/
Display.java
File metadata and controls
96 lines (81 loc) · 2.11 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
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
package aplicacion;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Display extends Canvas implements Runnable{
private static final long serialVersionUID= -1;
public static int WIDTH=800, HEIGHT = 600;
private boolean running= false;
public int FPS;
private Thread thread;
public static void main(String[] args) {
Display display = new Display();
JFrame frame = new JFrame();
frame.add(display);
frame.pack();
frame.setTitle("Space Invaders");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
display.start();
}
public synchronized void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public synchronized void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static GameScreen game;
public Display() {
this.setSize(WIDTH, HEIGHT);
this.setFocusable(true);
game = new GameScreen();
game.init(this);
}
@Override
public void run() {
long timer = System.currentTimeMillis();
long lastLoopTime = System.nanoTime();
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
int frames = 0;
this.createBufferStrategy(3);
BufferStrategy bs = this.getBufferStrategy();
while (running) {
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
double delta = updateLength / ((double) OPTIMAL_TIME);
draw(bs);
update(delta);
}
}
public void draw(BufferStrategy bs) {
do {
do {
Graphics2D g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH + 50, HEIGHT + 50);
game.draw(g);
g.dispose();
} while (bs.contentsRestored());
bs.show();
} while (bs.contentsLost());
}
public void update(double delta) {
game.update(delta);
}
}