Removed use of Refreshable and replaced with several methods which update the GUI using SwingUtilities.invokeLater().

While not as clean as using ChangeListener, it completely decouples the Model and View aspects,
and also avoids the need to manually paint() a hierarchy of components (a task best handled by Swing/AWT).
This commit is contained in:
Woody Folsom
2012-04-04 17:15:21 -04:00
parent 0f616a3cc6
commit b8df412740
9 changed files with 112 additions and 192 deletions

View File

@@ -3,18 +3,17 @@ package view;
import java.awt.GridLayout;
import java.net.URL;
import javax.security.auth.Refreshable;
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 implements Refreshable {
public class BoardPanel extends JPanel {
public static final URL BLUE_ICON = BoardPanel.class
.getResource("/img/blue.png");
@@ -26,59 +25,58 @@ public class BoardPanel extends JPanel implements Refreshable {
.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) {
public BoardPanel(Referee ref, TileSelectionPanel tsp) {
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));
BoardPanelMouseListener bpml = new BoardPanelMouseListener(r, c, referee.getPlayer());
//separate listener on each JLabel? This should probably be changed.
BoardPanelMouseListener bpml = new BoardPanelMouseListener(this, tsp, r,
c, referee.getPlayer());
// separate listener on each JLabel? This should probably be
// changed.
board[r][c].addMouseListener(bpml);
board[r][c].addMouseWheelListener(bpml);
add(board[r][c]);
}
}
refresh();
updateIcons();
}
@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));
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));
}
}
}
}
}
repaint();
});
}
}