summaryrefslogtreecommitdiff
path: root/src/tetris/gui/NextPieceDisplay.java
blob: 7c0b8b402d4bdda8a53b525d297f19143309cce5 (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
/*
 * 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.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;

import javax.swing.Timer;

import tetris.board.TetrisBoard;
import tetris.gui.animations.TetrisPieceAnimation;
import tetris.gui.images.ImageRes;
import tetris.model.IntPoint;
import tetris.piece.TPiece;
import tetris.piece.TetrisPiece;

/**
 * Panel that draws the tetris board.
 * 
 * @author Jesse Morgan <jesterpm@u.washington.edu>
 * @version 1.0 1 Dec 2009
 */
@SuppressWarnings("serial")
public class NextPieceDisplay extends TetrisPieceDisplay {
  /**
   * The Pencil Graphic.
   */
  private static final Image PENCIL = ImageRes.loadImage(ImageRes.WRITING_PENCIL);
  
  
  /**
   * The next piece.
   */
  private TetrisPiece my_next_piece;
  
  /**
   * Our animator.
   */
  private TetrisPieceAnimation my_animation;
  
  /**
   * Animation timer.
   */
  private final Timer my_timer;
  
 
  /**
   * Constructor. 
   */
  public NextPieceDisplay() {
    // Call parent
    super(TetrisPiece.TETRIS_PIECE_SIZE, TetrisPiece.TETRIS_PIECE_SIZE);
    
    my_animation = new TetrisPieceAnimation(new TPiece(0, 0));
    
    my_timer = new Timer(TetrisPieceAnimation.FRAME_RATE, new ActionListener() {
      public void actionPerformed(final ActionEvent the_event) {
        my_animation.stepAnimation();
        repaint();
      }
    });
  }
  
  /**
   * Update the next piece display.
   * 
   * @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;
      
      // If the next piece we know about is not the same next piece...
      if (tb.getNextPiece() != my_next_piece) {
        my_next_piece = tb.getNextPiece();
        my_bricks.clear();
        
        for (IntPoint p : my_next_piece.getBoardCoordinates()) {
          my_bricks.add(new IntPoint(p.getX() - my_next_piece.getX(),
                                     p.getY() - my_next_piece.getY() +
                                     TetrisBoard.NEW_PIECE_BUFFER));
        }
        
        my_animation = new TetrisPieceAnimation(my_next_piece);
        startAnimation();
      }
    
      repaint();
    }
  }
  
  @Override
  protected void paintComponent(final Graphics the_graphics) {
    super.paintComponent(the_graphics);
    
    if (my_animation.isRunning()) {
      final int x = my_animation.getX();
      final int y = my_animation.getY() - PENCIL.getHeight(null);
      the_graphics.drawImage(PENCIL, x, y, null);
      // Normally you'd draw the animation image here, but we just want the pencil to move.
    }
  }
  
  /**
   * Start the animation and timer.
   */
  private void startAnimation() {
    my_animation.start();
    my_timer.start();
  }

  
 
}