-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathGameComponent.java
More file actions
174 lines (158 loc) · 4 KB
/
GameComponent.java
File metadata and controls
174 lines (158 loc) · 4 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
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
163
164
165
166
167
168
169
170
171
172
173
174
package com.espol.mathhero2;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
public abstract class GameComponent extends JPanel
{
public static int WIDTH, HEIGHT;
protected BufferedImage background = null;
public int delay = 25;
/**
* Constructs a GameComponent with a width of w, and a height of h.
*
* @param w width
* @param h height
* @see JPanel
*/
public GameComponent(int w, int h)
{
super();
WIDTH = w;
HEIGHT = h;
setSize(WIDTH, HEIGHT);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground(Color.WHITE);
setVisible(true);
}
public void start()
{
Thread t = new Thread()
{
public void run()
{
while(true)
{
long time = System.currentTimeMillis();
if(background == null)
{
background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
background.getGraphics().setColor(Color.WHITE);
background.getGraphics().fillRect(0,0,WIDTH,HEIGHT);
}
requestFocus();
//update game state
standardUpdates();
update();
//draw stuff
standardDraw(getCanvas());
draw(background.getGraphics());
refreshImage();
time = System.currentTimeMillis()-time;
try
{
if(delay-(int)time > 0)
sleep(delay-(int)time);
}
catch(Exception ex)
{
}
}
}
};
try{Thread.sleep(500);}catch(Exception ex){}
t.start();
}
//get a blank image to draw onto
private Graphics getCanvas()
{
if(background == null)
{
background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
}
background.getGraphics().setColor(Color.WHITE);
background.getGraphics().fillRect(0,0,WIDTH,HEIGHT);
return background.getGraphics();
}
//take the canvas that you have drawn on and draw it onto the component
private void refreshImage()
{
if(background != null)
{
if(getGraphics() != null)
{
getGraphics().drawImage(background,0,0,null);
}
}
}
/**
* Creates a {@link JFrame} that contains this GameComponent.
*
* @return the {@link JFrame} created
*/
public JFrame makeTestWindow()
{
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(this);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
return frame;
}
/**
* Creates a fullscreen {@link JFrame} that contains this GameComponent.
* <br>*Note that the width and height of the component must be 640x480
*
* @return the {@link JFrame} created
*/
public JFrame makeFullScreenWindow()
{
JFrame frame = new JFrame();
GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
GraphicsConfiguration gc = device.getDefaultConfiguration();
DisplayMode oldDisplayMode = device.getDisplayMode();
DisplayMode newDisplayMode = new DisplayMode(640, 480, (oldDisplayMode.getBitDepth()), (oldDisplayMode.getRefreshRate()));
frame.getContentPane().setLayout(null);
frame.getContentPane().add(this, 0, 0);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.setResizable(false);
frame.setUndecorated(true);
frame.setVisible(true);
if(device.isFullScreenSupported())
{
device.setFullScreenWindow(frame);
device.setDisplayMode(newDisplayMode);
}
else
{
System.out.println("ARGS! NO FULLSCRENE!");
}
return frame;
}
/**
* Preforms the standard updates of the component.(Preformed befor {@link #update()})
*/
public void standardUpdates()
{
}
/**
* The method that draws the component.
*
* @param g the {@link Graphics} on which the component will be drawn
*/
public abstract void draw(Graphics g);
/**
* Draws the sandard parts of the component. (Preformed befor {@link #draw(Graphics)})
*
* @param g the {@link Graphics} on which the component will be drawn
*/
public void standardDraw(Graphics g)
{
}
/**
* The method that updates the component.
*/
public abstract void update();
}