-The rest of the game engine. I screwed up the commit last time.
This commit is contained in:
120
src/view/BoardPanel.java
Normal file
120
src/view/BoardPanel.java
Normal file
@@ -0,0 +1,120 @@
|
||||
package view;
|
||||
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.event.MouseListener;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.security.auth.Refreshable;
|
||||
import javax.swing.ImageIcon;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JPanel;
|
||||
|
||||
import model.Board;
|
||||
import model.Referee;
|
||||
|
||||
public class BoardPanel extends JPanel implements Refreshable {
|
||||
private class BoardPanelMouseListener implements MouseListener {
|
||||
|
||||
private final int col;
|
||||
private final int row;
|
||||
|
||||
public BoardPanelMouseListener(int r, int c) {
|
||||
row = r;
|
||||
col = c;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseClicked(MouseEvent e) {
|
||||
referee.getPlayer().setCell(row, col);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseEntered(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseExited(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mousePressed(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent arg0) {
|
||||
// Nothing.
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static final URL BLUE_ICON = ClassLoader
|
||||
.getSystemResource("img/blue.png");
|
||||
|
||||
public static final URL GREEN_ICON = ClassLoader
|
||||
.getSystemResource("img/green.png");
|
||||
public static final URL NONE_ICON = ClassLoader
|
||||
.getSystemResource("img/none.png");
|
||||
public static final URL RED_ICON = ClassLoader
|
||||
.getSystemResource("img/red.png");
|
||||
public static final URL YELLOW_ICON = ClassLoader
|
||||
.getSystemResource("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;
|
||||
|
||||
// private final ImageIcon[][] icons = new
|
||||
// ImageIcon[Board.NUM_ROWS][Board.NUM_COLS];
|
||||
|
||||
public BoardPanel(Referee ref) {
|
||||
referee = ref;
|
||||
setLayout(new GridLayout(Board.NUM_ROWS, Board.NUM_COLS));
|
||||
|
||||
for (int r = 0; r < Board.NUM_ROWS; r++) {
|
||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||
board[r][c] = new JLabel(new ImageIcon(NONE_ICON));
|
||||
board[r][c].addMouseListener(new BoardPanelMouseListener(r, c));
|
||||
add(board[r][c]);
|
||||
}
|
||||
}
|
||||
|
||||
refresh();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCurrent() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void refresh() {
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
repaint();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user