-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGameArena.java
More file actions
467 lines (404 loc) · 13.1 KB
/
GameArena.java
File metadata and controls
467 lines (404 loc) · 13.1 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
import java.util.*;
import java.util.Collections;
import java.util.concurrent.locks.*;
import javafx.scene.input.KeyEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javafx.application.Platform;
import javafx.animation.AnimationTimer;
import javafx.embed.swing.JFXPanel;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import java.awt.event.WindowEvent;
/**
* This class provides a simple window in which grahical objects can be drawn.
*
* Instances of the Ball and Rectangle classes can be added to an instance of this class to
* draw and animate simple shapes on the screen.
*
* @see Ball
* @see Rectangle
*
* @author Joe Finney
*/
public class GameArena
{
//Colours to chose from
public static final String BLACK = "#000000";
public static final String BLUE = "#0000ff";
public static final String CYAN = "#00ffff";
public static final String DARKGREY = "#a9a9a9";
public static final String GREY = "#808080";
public static final String GREEN = "#008000";
public static final String LIGHTGREY = "#d3d3d3";
public static final String MAGENTA = "#ff00ff";
public static final String ORANGE = "#ffa500";
public static final String PINK = "#ffc0cb";
public static final String RED = "#ff0000";
public static final String WHITE = "#ffffff";
public static final String YELLOW = "#ffff00";
// Size of window
private int arenaWidth;
private int arenaHeight;
private JFrame window;
private boolean exiting = false;
private final static int MAXIMUM_OBJECTS = 100000;
// Collections of primitives. These now relate 1:1 to JavaFX Nodes, since moving from AWT.
private List<Object> addList = new ArrayList<Object>();
private List<Object> removeList = new ArrayList<Object>();
private Map<Ball, javafx.scene.shape.Circle> balls = new HashMap<>();
private Map<Rectangle, javafx.scene.shape.Rectangle> rectangles = new HashMap<>();
private int objectCount;
// Basic button state
private boolean up = false;
private boolean down = false;
private boolean left = false;
private boolean right = false;
// JavaFX containers
private Scene scene;
private Group root;
private JFXPanel jfxPanel;
// Lock used to reduce flicker when rendering of large numbers of objects.
private ReentrantLock renderLock;
private boolean initialised;
/**
* Constructor. Creates an instance of the GameArena class, and displays a window on the
* screen upon which shapes can be drawn.
*
* @param width The width of the window, in pixels.
* @param height The height of the window, in pixels.
*/
public GameArena(int width, int height)
{
this(width, height, true);
}
/**
* Constructor. Creates an instance of the GameArena class, and displays a window on the
* screen upon which shapes can be drawn.
*
* @param width The width of the window, in pixels.
* @param height The height of the window, in pixels.
* @param createWindow Determines if a JFrame containing the GameArena should be created (true) or not (false).
*/
public GameArena(int width, int height, boolean createWindow)
{
this.arenaWidth = width;
this.arenaHeight = height;
this.objectCount = 0;
this.initialised = false;
// Create a lock to reduce flicker on rendering
renderLock = new ReentrantLock();
// Create a JavaFX canvas as a Swing panel.
jfxPanel = new JFXPanel();
jfxPanel.setPreferredSize(new java.awt.Dimension(width, height));
// Create a window, if necessary.
if (createWindow)
{
window = new JFrame();
window.setTitle("Let's Play!");
window.setContentPane(jfxPanel);
window.setResizable(false);
window.pack();
window.setVisible(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
root = new Group();
scene = new Scene(root, arenaWidth, arenaHeight, Color.BLACK);
renderLock.lock();
Platform.runLater(new Runnable() {
@Override
public void run() {
initFX();
}
});
}
private void initFX() {
EventHandler<KeyEvent> keyDownHandler = new EventHandler<KeyEvent>() {
public void handle(final KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.UP)
up = true;
if (keyEvent.getCode() == KeyCode.DOWN)
down = true;
if (keyEvent.getCode() == KeyCode.LEFT)
left = true;
if (keyEvent.getCode() == KeyCode.RIGHT)
right = true;
}
};
EventHandler<KeyEvent> keyUpHandler = new EventHandler<KeyEvent>() {
public void handle(final KeyEvent keyEvent) {
if (keyEvent.getCode() == KeyCode.UP)
up = false;
if (keyEvent.getCode() == KeyCode.DOWN)
down = false;
if (keyEvent.getCode() == KeyCode.LEFT)
left = false;
if (keyEvent.getCode() == KeyCode.RIGHT)
right = false;
}
};
scene.setOnKeyPressed(keyDownHandler);
scene.setOnKeyReleased(keyUpHandler);
jfxPanel.setScene(scene);
new AnimationTimer() {
@Override
public void handle(long now) {
frameUpdate();
}
}.start();
}
/**
* Close this GameArena window.
*
*/
public void exit()
{
this.exiting = true;
}
/**
* A method called by the operating system to draw onto the screen - <p><B>YOU DO NOT (AND SHOULD NOT) NEED TO CALL THIS METHOD.</b></p>
*/
private void frameUpdate ()
{
if (!this.initialised)
return;
if (this.exiting)
{
addList.clear();
removeList.clear();
rectangles.clear();
balls.clear();
objectCount = 0;
initialised = false;
root.getChildren().clear();
if (window != null)
window.dispatchEvent(new WindowEvent(window, WindowEvent.WINDOW_CLOSING));
this.exiting = false;
return;
}
// Optimise for the case where we have no data to render
if(objectCount == 0)
return;
// Remove any deleted objects from the scene.
renderLock.lock();
for (Object o: removeList)
{
if (o instanceof Ball)
{
Ball b = (Ball) o;
javafx.scene.shape.Circle c = balls.get(b);
root.getChildren().remove(c);
balls.remove(b);
}
if (o instanceof Rectangle)
{
Rectangle r = (Rectangle) o;
javafx.scene.shape.Rectangle rectangle = rectangles.get(r);
root.getChildren().remove(rectangle);
rectangles.remove(r);
}
}
removeList.clear();
// Add any new objects to the scene.
for (Object o: addList)
{
if (o instanceof Ball)
{
Ball b = (Ball) o;
javafx.scene.shape.Circle c = new javafx.scene.shape.Circle(0,0,b.getSize());
root.getChildren().add(c);
balls.put(b, c);
}
if (o instanceof Rectangle)
{
Rectangle r = (Rectangle) o;
javafx.scene.shape.Rectangle rectangle = new javafx.scene.shape.Rectangle(0, 0, r.getWidth(), r.getHeight());
root.getChildren().add(rectangle);
rectangles.put(r, rectangle);
}
}
addList.clear();
for(Map.Entry<Ball, javafx.scene.shape.Circle> entry : balls.entrySet())
{
Ball b = entry.getKey();
javafx.scene.shape.Circle c = entry.getValue();
c.setRadius(b.getSize());
c.setTranslateX(b.getXPosition());
c.setTranslateY(b.getYPosition());
c.setFill(getColourFromString(b.getColour()));
}
for(Map.Entry<Rectangle, javafx.scene.shape.Rectangle> entry : rectangles.entrySet())
{
Rectangle r = entry.getKey();
javafx.scene.shape.Rectangle rectangle = entry.getValue();
rectangle.setWidth(r.getWidth());
rectangle.setHeight(r.getHeight());
rectangle.setTranslateX(r.getXPosition() - r.getWidth()/2);
rectangle.setTranslateY(r.getYPosition() - r.getHeight()/2);
rectangle.setFill(getColourFromString(r.getColour()));
}
renderLock.unlock();
}
//
// Derive a Color object from a given string representation
//
private Color getColourFromString(String col)
{
Color colour = Color.web(col);
return colour;
}
/**
* Adds a given Ball to the GameArena.
* Once a Ball is added, it will automatically appear on the window.
*
* @param b the ball to add to the GameArena.
*/
public void addBall(Ball b)
{
synchronized (this)
{
if (objectCount > MAXIMUM_OBJECTS)
{
System.out.println("\n\n");
System.out.println(" ********************************************************* ");
System.out.println(" ***** Only 100000 Objects Supported per Game Arena! ***** ");
System.out.println(" ********************************************************* ");
System.out.println("\n");
System.out.println("-- Joe\n\n");
System.exit(0);
}
// Add this ball to the draw list. Initially, with a null JavaFX entry, which we'll fill in later to avoid cross-thread operations...
removeList.remove(b);
addList.add(b);
objectCount++;
}
}
/**
* Remove a Ball from the GameArena.
* Once a Ball is removed, it will no longer appear on the window.
*
* @param b the ball to remove from the GameArena.
*/
public void removeBall(Ball b)
{
synchronized (this)
{
addList.remove(b);
removeList.add(b);
objectCount--;
}
}
/**
* Adds a given rectangle to the GameArena.
* Once a Rectangle is added, it will automatically appear on the window.
*
* @param r the rectangle to add to the GameArena.
*/
public void addRectangle(Rectangle r)
{
synchronized (this)
{
if (objectCount > MAXIMUM_OBJECTS)
{
System.out.println("\n\n");
System.out.println(" ********************************************************* ");
System.out.println(" ***** Only 100000 Objects Supported per Game Arena! ***** ");
System.out.println(" ********************************************************* ");
System.out.println("\n");
System.out.println("-- Joe\n\n");
System.exit(0);
}
// Add this ball to the draw list. Initially, with a null JavaFX entry, which we'll fill in later to avoid cross-thread operations...
removeList.remove(r);
addList.add(r);
objectCount++;
}
}
/**
* Remove a Rectangle from the GameArena.
* Once a Rectangle is removed, it will no longer appear on the window.
*
* @param r the rectangle to remove from the GameArena.
*/
public void removeRectangle(Rectangle r)
{
synchronized (this)
{
addList.remove(r);
removeList.add(r);
objectCount--;
}
}
/**
* Pause for a 1/50 of a second.
* This method causes your program to delay for 1/50th of a second. You'll find this useful if you're trying to animate your application.
*/
public void pause()
{
this.initialised = true;
renderLock.unlock();
try { Thread.sleep(18); }
catch (Exception e) {};
renderLock.lock();
}
/**
* Gets the width of the GameArena window, in pixels.
* @return the width in pixels
*/
public int getArenaWidth()
{
return arenaWidth;
}
/**
* Gets the height of the GameArena window, in pixels.
* @return the height in pixels
*/
public int getArenaHeight()
{
return arenaHeight;
}
/**
* Determines if the user is currently pressing the cursor up button.
* @return true if the up button is pressed, false otherwise.
*/
public boolean upPressed()
{
return up;
}
/**
* Determines if the user is currently pressing the cursor down button.
* @return true if the down button is pressed, false otherwise.
*/
public boolean downPressed()
{
return down;
}
/**
* Determines if the user is currently pressing the cursor left button.
* @return true if the left button is pressed, false otherwise.
*/
public boolean leftPressed()
{
return left;
}
/**
* Determines if the user is currently pressing the cursor right button.
* @return true if the right button is pressed, false otherwise.
*/
public boolean rightPressed()
{
return right;
}
/**
* Acquires the JPanel containing this GameArena.
* @return The JPanel object containing this GameArena.
*/
public JFXPanel getPanel()
{
return jfxPanel;
}
}