Average Density: 0.10
  1 package nl.tudelft.jpacman.ui;
  2 
  3 import java.awt.Color;
  4 import java.awt.Dimension;
  5 import java.awt.Graphics;
  6 
  7 import javax.swing.JPanel;
  8 
  9 import nl.tudelft.jpacman.board.Board;
 10 import nl.tudelft.jpacman.board.Square;
 11 import nl.tudelft.jpacman.board.Unit;
 12 import nl.tudelft.jpacman.game.Game;
 13 
 14 /**
 15  * Panel displaying a game.
 16  *
 17  * @author Jeroen Roosen 
 18  *
 19  */
 20 class BoardPanel extends JPanel {
 21 
 22     /**
 23      * Default serialisation ID.
 24      */
 25     private static final long serialVersionUID = 1L;
 26 
 27     /**
 28      * The background colour of the board.
 29      */
 30     private static final Color BACKGROUND_COLOR = Color.BLACK;
 31 
 32     /**
 33      * The size (in pixels) of a square on the board. The initial size of this
 34      * panel will scale to fit a board with square of this size.
 35      */
 36     private static final int SQUARE_SIZE = 16;
 37 
 38     /**
 39      * The game to display.
 40      */
 41     private final Game game;
 42 
 43     /**
 44      * Creates a new board panel that will display the provided game.
 45      *
 46      * @param game
 47      *            The game to display.
 48      */
 49     BoardPanel(Game game) {
 50         super();
 51         assert game != null;
 52         this.game = game;
 53 
 54         Board board = game.getLevel().getBoard();
 55 
 56         int w = board.getWidth() * SQUARE_SIZE;
 57         int h = board.getHeight() * SQUARE_SIZE;
 58 
 59         Dimension size = new Dimension(w, h);
 60         setMinimumSize(size);
 61         setPreferredSize(size);
 62     }
 63 
 64     @Override
 65     public void paint(Graphics g) {
 66         assert g != null;
 67         render(game.getLevel().getBoard(), g, getSize());
 68     }
 69 
 70     /**
 71      * Renders the board on the given graphics context to the given dimensions.
 72      *
 73      * @param board
 74      *            The board to render.
 75      * @param graphics
 76      *            The graphics context to draw on.
 77      * @param window
 78      *            The dimensions to scale the rendered board to.
 79      */
 80     private void render(Board board, Graphics graphics, Dimension window) {
 81         int cellW = window.width / board.getWidth();
 82         int cellH = window.height / board.getHeight();
 83 
 84         graphics.setColor(BACKGROUND_COLOR);
 85         graphics.fillRect(0, 0, window.width, window.height);
 86 
 87         for (int y = 0; y < board.getHeight(); y++) {
 88             for (int x = 0; x < board.getWidth(); x++) {
 89                 int cellX = x * cellW;
 90                 int cellY = y * cellH;
 91                 Square square = board.squareAt(x, y);
 92                 render(square, graphics, cellX, cellY, cellW, cellH);
 93             }
 94         }
 95     }
 96 
 97     /**
 98      * Renders a single square on the given graphics context on the specified
 99      * rectangle.
100      *
101      * @param square
102      *            The square to render.
103      * @param graphics
104      *            The graphics context to draw on.
105      * @param x
106      *            The x position to start drawing.
107      * @param y
108      *            The y position to start drawing.
109      * @param width
110      *            The width of this square (in pixels.)
111      * @param height
112      *            The height of this square (in pixels.)
113      */
114     private void render(Square square, Graphics graphics, int x, int y, int width, int height) {
115         square.getSprite().draw(graphics, x, y, width, height);
116         for (Unit unit : square.getOccupants()) {
117             unit.getSprite().draw(graphics, x, y, width, height);
118         }
119     }
120 }