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

package tetris.gui;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;

import tetris.board.TetrisBoard;

/**
 * JPanel of info about the game.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 11 December 2009
 */
@SuppressWarnings("serial")
public class InfoPanel extends JPanel implements Observer {
  // Private Constants
  /**
   * The wording of the new game button.
   */
  private static final String NEWGAME_LABEL = "New Game";
  
  /**
   * The wording of the pause button.
   */
  private static final String PAUSE_LABEL = "Pause";
  
  /**
   * The wording of the unpause button.
   */
  private static final String UNPAUSE_LABEL = "Unpause";
  
  // Obnoxious UI layout constants follow
  /**
   * Space between the top of the panel and the Score.
   */
  private static final int UPPER_SPACING = 18;
  
  /**
   * Space between the level and the next piece.
   */
  private static final int MIDDLE_SPACING = 38;
  
  /**
   * Size of the score.
   */
  private static final float SCORE_FONT_SIZE = 32f;
  
  /**
   * Size of the level.
   */
  private static final float LEVEL_FONT_SIZE = 35f;
  
  /**
   * The TetrisBoard we're displaying info for. 
   */
  private final TetrisBoard my_board;
  
  /**
   * The score label.
   */
  private final JLabel my_score_label;
  
  /**
   * The level label.
   */
  private final JLabel my_level_label;
  
  /**
   * Construct the Info Panel.
   * 
   * @param the_board The tetris board we belong to.
   */
  public InfoPanel(final TetrisBoard the_board) {
    super();
    
    my_board = the_board;
    the_board.addObserver(this);
    
    // Setup Panel
    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    setOpaque(false);
    
    // Create Components
    my_score_label = new JLabel("0");
    my_level_label = new JLabel("Level 1");
    
    setupPanel();
  }
 
  /**
   * Handle notifications.
   * 
   * @param the_observable The TetrisBoard.
   * @param the_arg Unused argument.
   */
  public void update(final Observable the_observable, final Object the_arg) {
    if (the_observable instanceof TetrisBoard) {
      final TetrisBoard tb = (TetrisBoard) the_observable;
      
      my_score_label.setText(String.valueOf(tb.getScore()));
      
      my_level_label.setText("Level " + tb.getLevel());
    }
  }
  
  
  /**
   * Helper method to setup the east side.
   */
  private void setupPanel() {   
    Font base_font;
    
    // Load the font.
    try {
      final InputStream fis = getClass().getResourceAsStream("/tetris/gui/handwriting.ttf");
      base_font = Font.createFont(Font.TRUETYPE_FONT, fis);
      fis.close();
      
    } catch (final FontFormatException the_exception) {
      base_font = Font.getFont(Font.SANS_SERIF);
      
    } catch (final IOException the_exception) {
      base_font = Font.getFont(Font.SANS_SERIF);
    }
   
    // Add a Strut to move everything onto a line.
    add(Box.createVerticalStrut(UPPER_SPACING));
    
    // Display the Score  
    my_score_label.setFont(base_font.deriveFont(SCORE_FONT_SIZE));
    my_score_label.setAlignmentX(CENTER_ALIGNMENT);
    add(my_score_label);

    
    // Display the level
    my_level_label.setFont(base_font.deriveFont(LEVEL_FONT_SIZE));
    my_level_label.setAlignmentX(CENTER_ALIGNMENT);
    add(my_level_label);
    
    // Add a bit of space between the level and the next piece display.
    add(Box.createVerticalStrut(MIDDLE_SPACING));
    
    // Add the next piece component
    final NextPieceDisplay nextpiece = new NextPieceDisplay();
    add(nextpiece);
    nextpiece.setAlignmentX(CENTER_ALIGNMENT);
    my_board.addObserver(nextpiece);
    
 
    
    // New Game Button
    final WrittenButton new_game = new WrittenButton();
    new_game.setText(NEWGAME_LABEL);
    new_game.setAlignmentX(CENTER_ALIGNMENT);
    add(new_game);
   
    // Pause Game Button
    final WrittenButton pause_game = new WrittenButton();
    pause_game.setText(PAUSE_LABEL);
    pause_game.setAlignmentX(CENTER_ALIGNMENT);
    add(pause_game);
    
    // Setup Action Listeners
    
    // New Game
    new_game.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent the_event) {
        my_board.newGame();        
      }
    });
    
    // Pause Game
    pause_game.addActionListener(new ActionListener() {
      public void actionPerformed(final ActionEvent the_event) {
        final WrittenButton b = (WrittenButton) the_event.getSource();
        
        // Do nothing if the game is over.
        if (my_board.isGameOver()) {
          return;
        }
        
        if (b.getText().equals(PAUSE_LABEL)) {
          my_board.setPaused(true);
          b.setText(UNPAUSE_LABEL);
          
        } else {
          my_board.setPaused(false);
          b.setText(PAUSE_LABEL);
        }
      }
    });
  }
}