-
Notifications
You must be signed in to change notification settings - Fork 13
/
GameArena.java
757 lines (653 loc) · 18.6 KB
/
GameArena.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
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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Class;
import java.lang.reflect.*;
/**
* This class provides a simple window in which grahical objects can be drawn.
* @author Joe Finney
*/
public class GameArena extends JPanel implements Runnable, KeyListener, MouseListener, MouseMotionListener
{
// Size of playarea
private JFrame frame;
private int arenaWidth;
private int arenaHeight;
private boolean exiting = false;
private ArrayList<Object> things = new ArrayList<Object>();
private HashMap<String, Color> colours = new HashMap<>();
private boolean up = false;
private boolean down = false;
private boolean left = false;
private boolean right = false;
private boolean shift = false;
private boolean space = false;
private boolean esc = false;
private boolean enter = false;
private boolean[] alphasPressed = new boolean[26];
private final static int asciiAZOffset = 65;
private final static int alphabetLength = 25;
private boolean leftMouse = false;
private boolean rightMouse = false;
private int mouseX = 0;
private int mouseY = 0;
private BufferedImage buffer;
private Graphics2D graphics;
private Map<RenderingHints.Key, Object> renderingHints;
private boolean rendered = false;
private Image backgroundImage = null;
/**
* Create a view of a GameArena.
*
* @param width The width of the playing area, in pixels.
* @param height The height of the playing area, in pixels.
*/
public GameArena(int width, int height)
{
this.init(width, height, true);
}
/**
* Create a view of a GameArena.
*
* @param width The width of the playing area, in pixels.
* @param height The height of the playing area, in pixels.
* @param createWindow Defines if a window should be created to host this GameArena. @see getPanel.
*/
public GameArena(int width, int height, boolean createWindow)
{
this.init(width, height, createWindow);
}
/**
* Internal initialisation method - called by constructor methods.
*/
private void init(int width, int height, boolean createWindow)
{
if (createWindow)
{
this.frame = new JFrame();
frame.setTitle("Let's Play!");
frame.setSize(width, height);
frame.setResizable(false);
frame.setBackground(Color.BLACK);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(this);
frame.setVisible(true);
}
this.setSize(width, height);
// Add standard colours.
colours.put("BLACK", Color.BLACK);
colours.put("BLUE", Color.BLUE);
colours.put("CYAN", Color.CYAN);
colours.put("DARKGREY", Color.DARK_GRAY);
colours.put("GREY", Color.GRAY);
colours.put("GREEN", Color.GREEN);
colours.put("LIGHTGREY", Color.LIGHT_GRAY);
colours.put("MAGENTA", Color.MAGENTA);
colours.put("ORANGE", Color.ORANGE);
colours.put("PINK", Color.PINK);
colours.put("RED", Color.RED);
colours.put("WHITE", Color.WHITE);
colours.put("YELLOW", Color.YELLOW);
// Setup graphics rendering hints for quality
renderingHints = new HashMap<>();
renderingHints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
renderingHints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);
renderingHints.put(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON);
renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
renderingHints.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
renderingHints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
try {
Class rh = Class.forName("java.awt.RenderingHints");
RenderingHints.Key key = (RenderingHints.Key) rh.getField("KEY_RESOLUTION_VARIANT").get(null);
Object value = rh.getField("VALUE_RESOLUTION_VARIANT_DPI_FIT").get(null);
renderingHints.put(key, value);
}
catch (Exception e){}
Thread t = new Thread(this);
t.start();
this.addMouseListener(this);
this.addMouseMotionListener(this);
if (frame != null)
frame.addKeyListener(this);
}
public void run() {
try {
while (!exiting) {
this.repaint();
Thread.sleep(10);
}
} catch (InterruptedException iex) {}
if (frame != null)
frame.dispatchEvent(new WindowEvent(frame, WindowEvent.WINDOW_CLOSING));
}
/**
* Update the size of the GameArena.
*
* @param width the new width of the window in pixels.
* @param height the new height of the window in pixels.
*/
public void setSize(int width, int height)
{
this.arenaWidth = width;
this.arenaHeight = height;
super.setSize(width,height);
if (frame != null)
frame.setSize(arenaWidth + frame.getInsets().left + frame.getInsets().right, arenaHeight + frame.getInsets().top + frame.getInsets().bottom);
}
/**
* Sets the operation that occurs when the user
* initiates a "close" on this frame.
* from the swing.JFrame class
*
* @param oepration the operation which should be performed when the
* user closes the frame
*/
public void setDefaultCloseOperation(int operation)
{
frame.setDefaultCloseOperation(operation);
}
/**
* Returns the operation that occurs when the user
* initiates a "close" on this frame.
* from the swing.JFrame class
*
* @return an integer indicating the window-close operation
*/
public int getDefaultCloseOperation()
{
return frame.getDefaultCloseOperation();
}
/**
* Set the background of the GameArena window to the specified image file.
* The image will be scaled to fit the size of the GameArena
*
* @param filename the filename of an image to use.
*/
public void setBackgroundImage(String filename)
{
Toolkit t=Toolkit.getDefaultToolkit();
backgroundImage = t.getImage(filename);
}
/**
* Retrieves the JPanel on which this gameArena is drawn, so that it can be integrated into
* a users application.
*
* n.b. This should only be called if this GameArena was constructed without its own JFrame
*
* @return the JPanel containing this GameArena.
*/
public JPanel getPanel()
{
return this;
}
/**
* 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>
*/
public void paint (Graphics gr)
{
Graphics2D window = (Graphics2D) gr;
if (!rendered)
{
this.setSize(arenaWidth, arenaHeight);
// Create a buffer the same size of the window, which we can reuse from frame to frame to improve performance.
buffer = new BufferedImage(arenaWidth, arenaHeight, BufferedImage.TYPE_INT_ARGB);
graphics = buffer.createGraphics();
graphics.setRenderingHints(renderingHints);
// Remember that we've completed this initialisation, so that we don't do it again...
rendered = true;
}
if (frame == null)
{
// Find the JFrame we have been added to, and attach a KeyListner
frame = (JFrame) SwingUtilities.getWindowAncestor(this);
if (frame != null)
frame.addKeyListener(this);
}
window.setRenderingHints(renderingHints);
synchronized (this)
{
if (!this.exiting)
{
graphics.clearRect(0,0, arenaWidth, arenaHeight);
if (backgroundImage != null)
graphics.drawImage(backgroundImage, 0, 0, arenaWidth, arenaHeight, 0, 0, backgroundImage.getWidth(null), backgroundImage.getHeight(null), null);
for (Object o : things)
{
if (o instanceof Ball)
{
Ball b = (Ball) o;
graphics.setColor(this.getColourFromString(b.getColour()));
graphics.fillOval((int)(b.getXPosition() - b.getSize()/2), (int)(b.getYPosition() - b.getSize()/2), (int)b.getSize(), (int)b.getSize());
}
if (o instanceof Rectangle)
{
Rectangle r = (Rectangle) o;
graphics.setColor(this.getColourFromString(r.getColour()));
graphics.fillRect((int)r.getXPosition(), (int)r.getYPosition(), (int)r.getWidth(), (int)r.getHeight());
}
if (o instanceof Line)
{
Line l = (Line) o;
graphics.setColor(this.getColourFromString(l.getColour()));
graphics.setStroke(new BasicStroke((float)l.getWidth()));
float sx = (float)l.getXStart();
float sy = (float)l.getYStart();
float ex = (float)l.getXEnd();
float ey = (float)l.getYEnd();
if (l.getArrowSize() > 0)
{
float arrowRatio = (float) (1.0 - ((l.getWidth() * l.getArrowSize()) / l.getLength()));
ex = sx + ((ex - sx) * arrowRatio);
ey = sy + ((ey - sy) * arrowRatio);
graphics.fillPolygon(l.getArrowX(), l.getArrowY(), 3);
}
graphics.draw(new Line2D.Float(sx,sy,ex,ey));
}
if (o instanceof Text)
{
Text t = (Text) o;
graphics.setFont(new Font("SansSerif", Font.BOLD, t.getSize()));
graphics.setColor(this.getColourFromString(t.getColour()));
graphics.drawString(t.getText(),(float)t.getXPosition(), (float)t.getYPosition());
}
}
}
window.drawImage(buffer, this.getInsets().left, this.getInsets().top, this);
}
}
//
// Shouldn't really handle colour this way, but the student's haven't been introduced
// to constants properly yet, hmmm....
//
private Color getColourFromString(String col)
{
Color c = colours.get(col.toUpperCase());
if (c == null && col.startsWith("#"))
{
int r = Integer.valueOf( col.substring( 1, 3 ), 16 );
int g = Integer.valueOf( col.substring( 3, 5 ), 16 );
int b = Integer.valueOf( col.substring( 5, 7 ), 16 );
c = new Color(r,g,b);
colours.put(col.toUpperCase(), c);
}
if (c == null)
c = Color.WHITE;
return c;
}
/**
* Adds a given Object to the drawlist, maintaining z buffering order.
*
* @param o the object to add to the drawlist.
*/
private void addThing(Object o, int layer)
{
boolean added = false;
if (exiting)
return;
synchronized (this)
{
if (things.size() > 100000)
{
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");
this.exit();
}
else
{
// Try to insert this object into the list.
for (int i=0; i<things.size(); i++)
{
int l = 0;
Object obj = things.get(i);
if (obj instanceof Ball)
l = ((Ball)obj).getLayer();
if (obj instanceof Rectangle)
l = ((Rectangle)obj).getLayer();
if (obj instanceof Line)
l = ((Line)obj).getLayer();
if (obj instanceof Text)
l = ((Text)obj).getLayer();
if (layer < l)
{
things.add(i,o);
added = true;
break;
}
}
// If there are no items in the list with an equivalent or higher layer, append this object to the end of the list.
if (!added)
things.add(o);
}
}
}
/**
* Remove an object from the drawlist.
*
* @param o the object to remove from the drawlist.
*/
private void removeObject(Object o)
{
synchronized (this)
{
things.remove(o);
}
}
/**
* 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)
{
this.addThing(b, b.getLayer());
}
/**
* 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)
{
this.addThing(r, r.getLayer());
}
/**
* Adds a given Line to the GameArena.
* Once a Line is added, it will automatically appear on the window.
*
* @param l the line to add to the GameArena.
*/
public void addLine(Line l)
{
this.addThing(l, l.getLayer());
}
/**
* Adds a given Text object to the GameArena.
* Once a Text object is added, it will automatically appear on the window.
*
* @param t the text object to add to the GameArena.
*/
public void addText(Text t)
{
this.addThing(t, t.getLayer());
}
/**
* 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)
{
this.removeObject(r);
}
/**
* 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)
{
this.removeObject(b);
}
/**
* Remove a Line from the GameArena.
* Once a Line is removed, it will no longer appear on the window.
*
* @param l the line to remove from the GameArena.
*/
public void removeLine(Line l)
{
this.removeObject(l);
}
/**
* Remove a Text object from the GameArena.
* Once a Text object is removed, it will no longer appear on the window.
*
* @param t the text object to remove from the GameArena.
*/
public void removeText(Text t)
{
this.removeObject(t);
}
/**
* Removes every object that has ever been added to the GameArena. Nothing
* should appear on the GameArena window after this has executed.
*/
public void clearGameArena() {
synchronized(this) {
things.clear();
}
}
/**
* 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()
{
try { Thread.sleep(20); }
catch (Exception e) {};
}
/**
* Internal method to check if ASCII code is A-Z.
* @param asciiVal the ascii character value to check if it is within A-Z ascii.
* @return true or false if the ascii character value is A-Z ascii.
*/
private boolean isAsciiAZ(int asciiVal)
{
if (asciiVal >= asciiAZOffset && asciiVal <= asciiAZOffset+alphabetLength)
return true;
return false;
}
/**
* Check if an ASCII A-Z or a-z letter key has been pressed.
* @param key the ASCII A-Z or a-z char to check if it has been pressed.
* @return whether or not that char has been pressed.
*/
public boolean letterPressed(char key)
{
if (Character.isLetter(key))
if(isAsciiAZ((int) Character.toUpperCase(key)))
return alphasPressed[(int) Character.toUpperCase(key) -asciiAZOffset];
return false;
}
public void keyPressed(KeyEvent e)
{
keyAction(e,true);
}
public void keyAction(KeyEvent e,boolean yn)
{
int code = e.getKeyCode();
if (code == KeyEvent.VK_UP)
up = yn;
if (code == KeyEvent.VK_DOWN)
down = yn;
if (code == KeyEvent.VK_LEFT)
left = yn;
if (code == KeyEvent.VK_RIGHT)
right = yn;
if (code == KeyEvent.VK_SPACE)
space = yn;
if (code == KeyEvent.VK_SHIFT)
shift = yn;
if (code == KeyEvent.VK_ESCAPE)
esc = yn;
if (code == KeyEvent.VK_ENTER)
enter = yn;
//ASCII A-Z check
if (isAsciiAZ(code))
alphasPressed[code-asciiAZOffset] = yn;
}
public void keyReleased(KeyEvent e){
keyAction(e,false);
}
public void keyTyped(KeyEvent e)
{
}
public void mousePressed(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON1)
this.leftMouse = true;
if (e.getButton() == MouseEvent.BUTTON3)
this.rightMouse = true;
}
public void mouseReleased(MouseEvent e)
{
if (e.getButton() == MouseEvent.BUTTON1)
this.leftMouse = false;
if (e.getButton() == MouseEvent.BUTTON3)
this.rightMouse = false;
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseMoved(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
}
public void mouseDragged(MouseEvent e)
{
mouseX = e.getX();
mouseY = e.getY();
}
/**
* 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;
}
/**
* Determines if the user is currently pressing the space bar.
* @return true if the space bar is pressed, false otherwise.
*/
public boolean spacePressed()
{
return space;
}
/**
* Determines if the user is currently pressing the Esc button.
* @return true if the esc button is pressed, false otherwise.
*/
public boolean escPressed()
{
return esc;
}
/**
* Determines if the user is currently pressing the enter button.
* @return true if the enter button is pressed, false otherwise.
*/
public boolean enterPressed()
{
return enter;
}
/**
* Determines if the user is currently pressing the shift key.
* @return true if the shift key is pressed, false otherwise.
*/
public boolean shiftPressed()
{
return shift;
}
/**
* Determines if the user is currently pressing the left mouse button.
* @return true if the left mouse button is pressed, false otherwise.
*/
public boolean leftMousePressed()
{
return leftMouse;
}
/**
* Determines if the user is currently pressing the right mouse button.
* @return true if the right mouse button is pressed, false otherwise.
*/
public boolean rightMousePressed()
{
return rightMouse;
}
/**
* Gathers location informaiton on the mouse pointer.
* @return the current X coordinate of the mouse pointer in the GameArena.
*/
public int getMousePositionX()
{
return mouseX;
}
/**
* Gathers location informaiton on the mouse pointer.
* @return the current Y coordinate of the mouse pointer in the GameArena.
*/
public int getMousePositionY()
{
return mouseY;
}
}