-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display.java
98 lines (73 loc) · 1.98 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
import java.awt.*;
import javax.swing.*;
public class Display extends Canvas implements Runnable {
private static final long SerialVersionUID = 1L;
public Thread thread;
public JFrame frame;
public static final String title = "3D renderer";
public static final int Width = 800;
public static final int Height = 600;
public static Boolean isRunning = false;
public Draw draw;
public Display(){
this.frame = new JFrame();
Dimension size = new Dimension(Width, Height);
this.frame.setPreferredSize(size);
this.draw = new Draw();
this.frame.setContentPane(draw);
}
public static void main(String[] args) {
Display display = new Display();
display.frame.setTitle(title);
display.frame.add(display);
display.frame.pack();
display.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
display.frame.setResizable(false);
display.frame.setVisible(true);
display.start();
}
public synchronized void start(){
this.isRunning = true;
this.thread = new Thread(this, "Display");
this.thread.start();
}
public synchronized void stop(){
isRunning = false;
try{
this.thread.join();
} catch(InterruptedException e){
System.out.println("InterruptedException");
}
}
@Override
public void run(){
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
int fps = 60;
final double ns = 1000000000.0/fps;
double delta = 0;
int frames = 0;
while(isRunning){
long now = System.currentTimeMillis();
delta += (now - lastTime)/ns;
lastTime = now;
while(delta >= 1){
update();
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
//this.frame.setTitle(this.title + " ¡ " + frames + " fps");
frames = 0;
}
}
stop();
}
private void render(){
this.draw.repaint();
}
private void update(){
}
}