Files
cs8803p4/src/view/BoardPanel.java
Woody Folsom 74b8eb4622 Revampled MainFrame layout to improve appearance/usability when the number of tiles is very large or small.
Fixed instantiation where a new MouseListener was used to each tile - now only one is used for all Tiles.
Fixed issue where the MouseListener was being added to each tile twice.
2012-04-14 12:02:17 -04:00

81 lines
2.1 KiB
Java

package view;
import java.awt.GridLayout;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import controller.BoardPanelMouseListener;
import model.Board;
import model.Referee;
public class BoardPanel extends JPanel {
public static final URL BLUE_ICON = BoardPanel.class
.getResource("/img/blue.png");
public static final URL GREEN_ICON = BoardPanel.class
.getResource("/img/green.png");
public static final URL NONE_ICON = BoardPanel.class
.getResource("/img/none.png");
public static final URL RED_ICON = BoardPanel.class
.getResource("/img/red.png");
public static final URL YELLOW_ICON = BoardPanel.class
.getResource("/img/yellow.png");
private static final long serialVersionUID = 1L;
private final JLabel[][] board = new JLabel[Board.NUM_ROWS][Board.NUM_COLS];
private final Referee referee;
public BoardPanel(Referee ref, TileSelectionPanel tsp) {
referee = ref;
setLayout(new GridLayout(Board.NUM_ROWS, Board.NUM_COLS));
BoardPanelMouseListener bpml = new BoardPanelMouseListener(tsp,
referee.getHumanPlayer());
ImageIcon noneIcon = new ImageIcon(NONE_ICON);
for (int r = 0; r < Board.NUM_ROWS; r++) {
for (int c = 0; c < Board.NUM_COLS; c++) {
Tile tile = new Tile(noneIcon, r, c);
board[r][c] = tile;
board[r][c].addMouseListener(bpml);
board[r][c].addMouseWheelListener(bpml);
add(board[r][c]);
}
}
updateIcons();
}
public void updateIcons() {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
for (int r = 0; r < Board.NUM_ROWS; r++) {
for (int c = 0; c < Board.NUM_COLS; c++) {
switch (referee.getTile(r, c)) {
case BLUE:
board[r][c].setIcon(new ImageIcon(BLUE_ICON));
break;
case GREEN:
board[r][c].setIcon(new ImageIcon(GREEN_ICON));
break;
case RED:
board[r][c].setIcon(new ImageIcon(RED_ICON));
break;
case YELLOW:
board[r][c].setIcon(new ImageIcon(YELLOW_ICON));
break;
default:
board[r][c].setIcon(new ImageIcon(NONE_ICON));
}
}
}
}
});
}
}