summaryrefslogtreecommitdiff
path: root/src/tetris/gui/BoardUI.java
blob: 2ea441c6fb44b73386ff53ba9b56434ae1a54f8a (plain)
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
/*
 * Jesse Morgan <jesterpm@u.washington.edu>
 * 
 * TCSS 305 - Autumn 2009
 * Tetris Project
 * 17 November 2009
 */

package tetris.gui;

import java.awt.Graphics;
import java.awt.Image;
import java.util.Observable;

import tetris.board.TetrisBoard;
import tetris.gui.images.ImageRes;

/**
 * Panel that draws the tetris board.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 23 November 2009
 */
@SuppressWarnings("serial")
public class BoardUI extends TetrisPieceDisplay {
  /**
   * Flag to indicate game over.
   */
  private boolean my_game_over;
  
  /**
   * Flag to indicate game paused.
   */
  private boolean my_game_paused;
  
  /**
   * The Game Over graphic.
   */
  private final Image my_gameover_graphic;
  
  /**
   * The Paused graphic.
   */
  private final Image my_paused_graphic;
  
  /**
   * Constructor. 
   * 
   * @param the_width Number of columns.
   * @param the_height Number of rows.
   * 
   */
  public BoardUI(final int the_width, final int the_height) {
    // Call parent
    super(the_width, the_height);
    
    my_game_over = false;
    my_game_paused = false;
    
    my_gameover_graphic = ImageRes.loadImage(ImageRes.GAME_OVER_LABEL);
    
    my_paused_graphic = ImageRes.loadImage(ImageRes.PAUSED_LABEL);
    
  }
  
  /**
   * Update the board.
   * 
   * @param the_observable The tetris board we receive updates for.
   * @param the_argument Unused argument.
   */
  public void update(final Observable the_observable, final Object the_argument) {
    if (the_observable instanceof TetrisBoard) {
      final TetrisBoard tb = (TetrisBoard) the_observable;
      
      my_bricks = tb.getTetrisBlocks();
      my_game_over = tb.isGameOver();
      my_game_paused = tb.isPaused();
      
      repaint();
    }
    
    
  }

  /**
   * Paint Game Over or Paused if needed.
   * 
   * @param the_graphics The Graphics to draw on (expected to be a Graphics2D).
   */
  protected void paintComponent(final Graphics the_graphics) {
    super.paintComponent(the_graphics);
    
    if (my_game_over) {
      the_graphics.drawImage(my_gameover_graphic,
                             0,
                             (getHeight() - my_gameover_graphic.getHeight(null)) / 2,
                             null);
      
    } else if (my_game_paused) {
      the_graphics.drawImage(my_paused_graphic,
                             0,
                             (getHeight() - my_paused_graphic.getHeight(null)) / 2,
                             null);
    }
  }

  
 
}