forked from redomar/JavaGame
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryWindow.java
More file actions
107 lines (84 loc) · 2.22 KB
/
InventoryWindow.java
File metadata and controls
107 lines (84 loc) · 2.22 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
97
98
99
100
101
102
103
104
105
106
107
package com.redomar.game.objects;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import com.redomar.game.menu.DedicatedJFrame;
public class InventoryWindow implements Runnable{
private static final int WIDTH = 160;
private static final int HEIGHT = (WIDTH / 3 * 2);
private static final int SCALE = 2;
private static final String NAME = "Inventory";
private static boolean running = false;
private static DedicatedJFrame frame;
private static InventoryHandler window;
public synchronized void start(){
running = true;
setFrame(new DedicatedJFrame(WIDTH, HEIGHT, SCALE, NAME));
new Thread(this, NAME).start();
}
public synchronized void stop(){
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 30D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
setWindow(new InventoryHandler(getFrame()));
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = false;
while (delta >= 1) {
ticks++;
delta -= 1;
shouldRender = true;
}
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (shouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
getFrame().getFrame().setTitle(
"Frames: " + frames + " Ticks: " + ticks);
frames = 0;
ticks = 0;
}
}
}
private void render() {
BufferStrategy bs = getFrame().getBufferStrategy();
if(bs == null){
getFrame().createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH*SCALE+10, HEIGHT*SCALE+10);
g.setColor(Color.WHITE);
g.drawString(NAME, 50, 50);
bs.show();
g.dispose();
}
public DedicatedJFrame getFrame() {
return frame;
}
public static void setFrame(DedicatedJFrame frame) {
InventoryWindow.frame = frame;
}
public static InventoryHandler getWindow() {
return window;
}
public static void setWindow(InventoryHandler inventoryHandler) {
InventoryWindow.window = inventoryHandler;
}
}