comparison game/Engine.java @ 60:4a984e3b27d2

Cleanups. Add a ad-hoc about box, it's ugly.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 23 Feb 2011 22:59:38 +0200
parents fd10a9422b60
children caf67c7e0814
comparison
equal deleted inserted replaced
59:fd10a9422b60 60:4a984e3b27d2
332 332
333 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false); 333 static final AudioFormat sfmt = new AudioFormat(22050, 16, 1, true, false);
334 SoundManager soundManager; 334 SoundManager soundManager;
335 Sound musa; 335 Sound musa;
336 336
337 boolean aboutEnabled = false, aboutSecret = false;
338 BufferedImage aboutImg = null;
339
337 IDMContainer widgets; 340 IDMContainer widgets;
338 341
339 public Engine() 342 public Engine()
340 { 343 {
341 // Initialize globals 344 // Initialize globals
347 // Load resources 350 // Load resources
348 try 351 try
349 { 352 {
350 ResourceLoader res = new ResourceLoader("graphics/board.jpg"); 353 ResourceLoader res = new ResourceLoader("graphics/board.jpg");
351 lautaBG = ImageIO.read(res.getStream()); 354 lautaBG = ImageIO.read(res.getStream());
355
356 res = new ResourceLoader("graphics/lavina.jpg");
357 aboutImg = ImageIO.read(res.getStream());
352 358
353 try { 359 try {
354 res = new ResourceLoader("graphics/font.ttf"); 360 res = new ResourceLoader("graphics/font.ttf");
355 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream()); 361 fontMain = Font.createFont(Font.TRUETYPE_FONT, res.getStream());
356 362
360 catch (FontFormatException e) 366 catch (FontFormatException e)
361 { 367 {
362 System.out.print("Could not initialize fonts.\n"); 368 System.out.print("Could not initialize fonts.\n");
363 } 369 }
364 370
365 // musa = soundManager.getSound("sounds/gamemusic.wav"); 371 musa = soundManager.getSound("sounds/gamemusic.wav");
366 } 372 }
367 catch (IOException e) 373 catch (IOException e)
368 { 374 {
369 JOptionPane.showMessageDialog(null, 375 JOptionPane.showMessageDialog(null,
370 e.getMessage(), 376 e.getMessage(),
376 382
377 // Create IDM GUI widgets 383 // Create IDM GUI widgets
378 widgets = new IDMContainer(); 384 widgets = new IDMContainer();
379 385
380 widgets.add(new BtnSwapPiece(0.75f, 0.60f)); 386 widgets.add(new BtnSwapPiece(0.75f, 0.60f));
387 widgets.add(new BtnAbout(0.75f, 0.75f));
381 widgets.add(new BtnNewGame(0.75f, 0.85f)); 388 widgets.add(new BtnNewGame(0.75f, 0.85f));
382 389
383 lauta = new GameBoard(new IDMPoint(0.09f, 0.18f), soundManager, 63); 390 lauta = new GameBoard(new IDMPoint(0.09f, 0.18f), soundManager, 63);
384 widgets.add(lauta); 391 widgets.add(lauta);
385 392
386 // Game 393 // Game
394 aboutEnabled = false;
387 startNewGame(); 395 startNewGame();
388 396
389 // Initialize event listeners 397 // Initialize event listeners
390 addKeyListener(this); 398 addKeyListener(this);
391 addMouseListener(this); 399 addMouseListener(this);
405 { 413 {
406 gameClock = 0; 414 gameClock = 0;
407 gameFrames = 0; 415 gameFrames = 0;
408 startTime = new Date().getTime(); 416 startTime = new Date().getTime();
409 lauta.startNewGame(); 417 lauta.startNewGame();
418 }
419
420 public void paintAbout(Graphics2D g)
421 {
422 int width = lautaDim.width / 2,
423 height = lautaDim.height / 4;
424
425 int x = (lautaDim.width - width) / 2,
426 y = (lautaDim.height - height) / 2;
427
428 g.setPaint(new Color(0.0f, 0.0f, 0.0f, 0.6f));
429 g.fill(new RoundRectangle2D.Float(x, y, width, height, 10, 10));
430
431
432 g.setFont(font1);
433 g.setPaint(Color.white);
434
435 g.drawString("RistiPolku v0.5", x + 10, y + 25);
436 if (aboutSecret)
437 {
438 g.drawImage(aboutImg, x + 15, y + 35, aboutImg.getWidth(), aboutImg.getHeight(), null);
439 g.drawString("Dedicated to my", x + 225, y + 55);
440 g.drawString("favorite woman", x + 225, y + 85);
441 g.drawString("in the world.", x + 225, y + 115);
442 g.drawString("- Matti", x + 350, y + 155);
443 }
444 else
445 {
446 g.drawString("(c) Copyright 2011 Matti 'ccr' Hämäläinen", x + 10, y + 55);
447 g.drawString("Ohjelmointiprojekti Java-kurssille T740306", x + 10, y + 105);
448 }
410 } 449 }
411 450
412 public void paintComponent(Graphics g) 451 public void paintComponent(Graphics g)
413 { 452 {
414 Graphics2D g2 = (Graphics2D) g; 453 Graphics2D g2 = (Graphics2D) g;
482 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220); 521 g2.drawString(""+ String.format("%05d", lauta.gameScore), dim.width - 230, 220);
483 522
484 g2.setFont(font1); 523 g2.setFont(font1);
485 long currTime = new Date().getTime(); 524 long currTime = new Date().getTime();
486 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20); 525 g2.drawString("fps = "+ ((gameFrames * 1000) / (currTime - startTime)), dim.width - 120, 20);
487 526
527 if (aboutEnabled)
528 {
529 paintAbout(g2);
530 }
488 } 531 }
489 532
490 public void startThreads() 533 public void startThreads()
491 { 534 {
492 System.out.print("startThreads()\n"); 535 System.out.print("startThreads()\n");
517 public void mouseEntered(MouseEvent e) { } 560 public void mouseEntered(MouseEvent e) { }
518 public void mouseExited(MouseEvent e) { } 561 public void mouseExited(MouseEvent e) { }
519 562
520 public void mousePressed(MouseEvent e) 563 public void mousePressed(MouseEvent e)
521 { 564 {
522 widgets.mousePressed(e); 565 if (!aboutEnabled)
566 widgets.mousePressed(e);
523 } 567 }
524 568
525 public void mouseReleased(MouseEvent e) 569 public void mouseReleased(MouseEvent e)
526 { 570 {
527 widgets.mouseReleased(e); 571 if (!aboutEnabled)
572 widgets.mouseReleased(e);
528 } 573 }
529 574
530 public void mouseClicked(MouseEvent e) 575 public void mouseClicked(MouseEvent e)
531 { 576 {
532 System.out.print("mouseClicked()\n");
533 if (!hasFocus()) 577 if (!hasFocus())
534 { 578 {
535 System.out.print("requesting focus\n"); 579 System.out.print("Requesting focus\n");
536 requestFocus(); 580 requestFocus();
537 } 581 }
538 else 582 if (!aboutEnabled)
539 { 583 {
540 lauta.mouseClicked(e); 584 lauta.mouseClicked(e);
541 } 585 }
542 } 586 }
543 587
554 { 598 {
555 } 599 }
556 600
557 public void keyPressed(KeyEvent e) 601 public void keyPressed(KeyEvent e)
558 { 602 {
559 // Handle keyboard input 603 if (aboutEnabled)
560 if (lauta.keyPressed(e)) 604 {
561 return; 605 if (e.getKeyCode() == KeyEvent.VK_L)
562 606 {
563 widgets.keyPressed(e); 607 aboutSecret = true;
608 }
609 else
610 {
611 aboutEnabled = false;
612 aboutSecret = false;
613 }
614 }
615 else
616 {
617 // Handle keyboard input
618 if (lauta.keyPressed(e))
619 return;
620
621 widgets.keyPressed(e);
622 }
564 } 623 }
565 624
566 public void run() 625 public void run()
567 { 626 {
568 while (animEnable) 627 while (animEnable)
613 public void clicked() 672 public void clicked()
614 { 673 {
615 lauta.pieceSwapCurrent(); 674 lauta.pieceSwapCurrent();
616 } 675 }
617 } 676 }
677
678 class BtnAbout extends IDMButton
679 {
680 public BtnAbout(float x, float y)
681 {
682 super(x, y, KeyEvent.VK_A, font1, "Tietoja");
683 }
684
685 public void clicked()
686 {
687 aboutEnabled = true;
688 }
689 }
618 } 690 }