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

package tetris.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.List;
import java.util.Observer;

import javax.swing.JPanel;

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

/**
 * Abstract component capable of drawing out tetris pieces.
 * 
 * @author Jesse Morgan <jesse@jesterpm.net>
 * @version 1.0 1 Dec 2009
 */
@SuppressWarnings("serial")
public abstract class TetrisPieceDisplay extends JPanel implements Observer {
  // Private contants
  /**
   * Pixel size of one tetris piece.
   */
  private static final int BRICK_SIZE = 27;
  
  //Private fields
  /**
   * List of the bricks on the screen.
   */
  protected List<IntPoint> my_bricks;
  
  /**
   * Storage for the brick image.
   */
  private final Image my_brick_image;
  
  /**
   * Constructor. 
   * 
   * @param the_width Number of columns.
   * @param the_height Number of rows.
   */
  public TetrisPieceDisplay(final int the_width, final int the_height) {
    // Call parent
    super();
    
    // Load brick image
    my_brick_image = ImageRes.loadImage(ImageRes.TETRIS_BLOCK);
    
    // Create our brick list
    my_bricks = new ArrayList<IntPoint>();
    
    // Set some hints for the layout manager
    final Dimension d = new Dimension(the_width * my_brick_image.getWidth(null),
                                the_height * my_brick_image.getHeight(null));
    setPreferredSize(d);
    setMinimumSize(d);
    setMaximumSize(d);
    setOpaque(false);
  }

  @Override
  protected void paintComponent(final Graphics the_graphics) {
    super.paintComponent(the_graphics);
   
    for (IntPoint brick : my_bricks) {
      the_graphics.drawImage(my_brick_image,
                             brick.getX() * BRICK_SIZE,
                             (brick.getY() - TetrisBoard.NEW_PIECE_BUFFER) * BRICK_SIZE,
                              null);
    }
  }
}