comparison game/Engine.java @ 49:e6da5c71be28

Add more code to IDM widgets, do preliminary work for integrating them.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 22 Feb 2011 10:52:08 +0200
parents f13bab4cccd3
children 496e616ff09d
comparison
equal deleted inserted replaced
48:f13bab4cccd3 49:e6da5c71be28
54 { 54 {
55 } 55 }
56 } 56 }
57 */ 57 */
58 58
59 class IDMWidget
60 {
61 int keyCode;
62
63 public IDMWidget()
64 {
65 keyCode = -1;
66 }
67
68 public void paint(Graphics2D g)
69 {
70 }
71
72 public boolean contains(Point pos)
73 {
74 return false;
75 }
76
77 public void mousePressed(MouseEvent e)
78 {
79 }
80
81 public void mouseReleased(MouseEvent e)
82 {
83 if (contains(e.getPoint()))
84 {
85 clicked();
86 }
87 }
88
89 // Generic key handler
90 public boolean keyHandler(KeyEvent e)
91 {
92 if (e.getKeyCode() == keyCode)
93 {
94 clicked();
95 return true;
96 }
97 else
98 return false;
99 }
100
101 public void clicked()
102 {
103 }
104 }
105
106 class IDMButton extends IDMWidget
107 {
108 enum State { FOCUSED, PRESSED, NORMAL }
109 State state;
110 BufferedImage imgUp, imgPressed, img;
111 Point pos;
112
113 public IDMButton(float x, float y, int key, String text)
114 {
115 try
116 {
117 ResourceLoader res = new ResourceLoader("graphics/button1_up.png");
118 imgUp = ImageIO.read(res.getStream());
119
120 res = new ResourceLoader("graphics/button1_down.png");
121 imgPressed = ImageIO.read(res.getStream());
122 }
123 catch (IOException e)
124 {
125 System.out.print(e.getMessage());
126 }
127
128 keyCode = key;
129 setState(State.NORMAL);
130 }
131
132 private void setState(State newState)
133 {
134 state = newState;
135 if (state == State.PRESSED)
136 img = imgPressed;
137 else
138 img = imgUp;
139 }
140
141 public void paint(Graphics2D g)
142 {
143 g.drawImage(img, pos.x, pos.y, null);
144 }
145
146 public boolean contains(Point where)
147 {
148 return (where.x >= pos.x && where.y >= pos.y &&
149 where.x < pos.x + img.getWidth() &&
150 where.y < pos.y + img.getHeight());
151 }
152
153 public void clicked()
154 {
155 }
156 }
157
158
159 class GameBoard extends IDMWidget 59 class GameBoard extends IDMWidget
160 { 60 {
161 public static final int boardSize = 9; 61 public static final int boardSize = 9;
162 public static final int boardMiddle = 4; 62 public static final int boardMiddle = 4;
163 Piece[][] board; 63 Piece[][] board;
308 System.out.print("GameOver!\n"); 208 System.out.print("GameOver!\n");
309 } 209 }
310 } 210 }
311 } 211 }
312 212
313 public boolean keyHandler(KeyEvent e) 213 public boolean keyPressed(KeyEvent e)
314 { 214 {
315 switch (e.getKeyCode()) 215 switch (e.getKeyCode())
316 { 216 {
317 case KeyEvent.VK_LEFT: 217 case KeyEvent.VK_LEFT:
318 case KeyEvent.VK_UP: 218 case KeyEvent.VK_UP:
345 Font fontMain, font1, font2; 245 Font fontMain, font1, font2;
346 GameBoard lauta = null; 246 GameBoard lauta = null;
347 BufferedImage lautaBG = null, lautaBGScaled = null; 247 BufferedImage lautaBG = null, lautaBGScaled = null;
348 Dimension lautaDim; 248 Dimension lautaDim;
349 249
350
351 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); 250 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
352 SoundManager soundManager; 251 SoundManager soundManager;
353 Sound musa; 252 Sound musa;
354 253
254 IDMContainer widgets;
255 BtnNewGame btnNewGame;
256
355 public Engine() 257 public Engine()
356 { 258 {
357 // Initialize globals 259 // Initialize globals
358 System.out.print("Engine() constructor\n"); 260 System.out.print("Engine() constructor\n");
359 261
360 gameClock = 0;
361 gameFrames = 0;
362 startTime = new Date().getTime();
363
364 // Sound system 262 // Sound system
365 soundManager = new SoundManager(sfmt, 16); 263 soundManager = new SoundManager(sfmt, 16);
366 264
367 // Load resources 265 // Load resources
368 try 266 try
391 JOptionPane.ERROR_MESSAGE); 289 JOptionPane.ERROR_MESSAGE);
392 290
393 System.out.print(e.getMessage()); 291 System.out.print(e.getMessage());
394 } 292 }
395 293
396 // Initialize game components 294 // UI IDM widgets
397 lauta = new GameBoard(soundManager); 295 widgets = new IDMContainer();
296 btnNewGame = new BtnNewGame();
297 widgets.add(btnNewGame);
298
299 // Game
300 startNewGame();
301
302 // Initialize event listeners
398 addKeyListener(this); 303 addKeyListener(this);
399 addMouseListener(this); 304 addMouseListener(this);
400 305
401 // Get initial focus 306 // Get initial focus
402 if (!hasFocus()) 307 if (!hasFocus())
406 } 311 }
407 312
408 soundManager.play(musa); 313 soundManager.play(musa);
409 } 314 }
410 315
316 public void startNewGame()
317 {
318 gameClock = 0;
319 gameFrames = 0;
320 startTime = new Date().getTime();
321
322 lauta = new GameBoard(soundManager);
323 }
411 324
412 public void paintComponent(Graphics g) 325 public void paintComponent(Graphics g)
413 { 326 {
414 Graphics2D g2 = (Graphics2D) g; 327 Graphics2D g2 = (Graphics2D) g;
415 328
431 344
432 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null); 345 gimg.drawImage(lautaBG, 0, 0, dim.width, dim.height, null);
433 lautaDim = dim; 346 lautaDim = dim;
434 347
435 System.out.print("scale changed\n"); 348 System.out.print("scale changed\n");
349
350 btnNewGame.move(dim.width - 200, dim.height - 100);
436 } 351 }
437 352
438 // Background, pieces 353 // Background, pieces
439 g2.drawImage(lautaBGScaled, 0, 0, null); 354 g2.drawImage(lautaBGScaled, 0, 0, null);
440 lauta.paint(g2, 100, 150, 60); 355 lauta.paint(g2, 100, 150, 60);
441 356
357 btnNewGame.paint(g2);
358
442 // Scores 359 // Scores
443 g2.setFont(font1); 360 g2.setFont(font1);
444 g2.setPaint(Color.white); 361 g2.setPaint(Color.white);
445 362
446 363
476 soundManager.close(); 393 soundManager.close();
477 } 394 }
478 395
479 public void mouseEntered(MouseEvent e) { } 396 public void mouseEntered(MouseEvent e) { }
480 public void mouseExited(MouseEvent e) { } 397 public void mouseExited(MouseEvent e) { }
481 public void mousePressed(MouseEvent e) { } 398
482 public void mouseReleased(MouseEvent e) { } 399 public void mousePressed(MouseEvent e)
400 {
401 widgets.mousePressed(e);
402 }
403
404 public void mouseReleased(MouseEvent e)
405 {
406 widgets.mouseReleased(e);
407 }
483 408
484 public void mouseClicked(MouseEvent e) 409 public void mouseClicked(MouseEvent e)
485 { 410 {
486 System.out.print("mouseClicked()\n"); 411 System.out.print("mouseClicked()\n");
487 if (!hasFocus()) 412 if (!hasFocus())
500 } 425 }
501 426
502 public void keyPressed(KeyEvent e) 427 public void keyPressed(KeyEvent e)
503 { 428 {
504 // Handle keyboard input 429 // Handle keyboard input
505 if (lauta.keyHandler(e)) 430 if (lauta.keyPressed(e))
506 return; 431 return;
507 432
508 switch (e.getKeyCode()) 433 widgets.keyPressed(e);
509 {
510 case KeyEvent.VK_ESCAPE:
511 break;
512 }
513 } 434 }
514 435
515 public void run() 436 public void run()
516 { 437 {
517 while (animEnable) 438 while (animEnable)
535 } 456 }
536 catch (InterruptedException x) { 457 catch (InterruptedException x) {
537 } 458 }
538 } 459 }
539 } 460 }
461
462 class BtnNewGame extends IDMButton
463 {
464 public BtnNewGame()
465 {
466 super(0, 0, KeyEvent.VK_ESCAPE, font1, "New Game");
467 }
468
469 public void clicked()
470 {
471 startNewGame();
472 }
473 }
540 } 474 }