From b8df41274080cce3454c5bd52dfcf66ec3325d32 Mon Sep 17 00:00:00 2001 From: Woody Folsom Date: Wed, 4 Apr 2012 17:15:21 -0400 Subject: [PATCH] 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). --- src/controller/BoardPanelMouseListener.java | 11 +++- src/controller/PlayerController.java | 29 +++------ src/controller/TSPMouseListener.java | 2 +- src/model/Referee.java | 35 ++--------- src/view/BoardPanel.java | 70 ++++++++++----------- src/view/MainFrame.java | 50 +++------------ src/view/MessagePanel.java | 21 +++---- src/view/ScorePanel.java | 21 +++---- src/view/TileSelectionPanel.java | 65 +++++++++---------- 9 files changed, 112 insertions(+), 192 deletions(-) diff --git a/src/controller/BoardPanelMouseListener.java b/src/controller/BoardPanelMouseListener.java index 58b7ecc..741c5a0 100644 --- a/src/controller/BoardPanelMouseListener.java +++ b/src/controller/BoardPanelMouseListener.java @@ -5,21 +5,29 @@ import java.awt.event.MouseListener; import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; +import view.BoardPanel; +import view.TileSelectionPanel; + public class BoardPanelMouseListener implements MouseListener, MouseWheelListener { private final int col; private final int row; + private final BoardPanel boardPanel; private final PlayerController pc; + private final TileSelectionPanel tsp; - public BoardPanelMouseListener(int row, int col, PlayerController pc) { + public BoardPanelMouseListener(BoardPanel boardPanel, TileSelectionPanel tsp, int row, int col, PlayerController pc) { this.row = row; this.col = col; this.pc = pc; + this.boardPanel = boardPanel; + this.tsp = tsp; } @Override public void mouseClicked(MouseEvent e) { pc.setCell(row, col); + boardPanel.updateIcons(); } @Override @@ -49,6 +57,7 @@ public class BoardPanelMouseListener implements MouseListener, MouseWheelListene } else if (e.getWheelRotation() < 0) { pc.decrementColor(); } + tsp.updateBorders(); } } \ No newline at end of file diff --git a/src/controller/PlayerController.java b/src/controller/PlayerController.java index 506f972..58cbdf2 100644 --- a/src/controller/PlayerController.java +++ b/src/controller/PlayerController.java @@ -7,11 +7,10 @@ import model.Move; public class PlayerController { private final CellPointer cell = new CellPointer(0, 0); - - private boolean current = true; + private boolean ready = false; private TileColor color = TileColor.BLUE; - + public void denyMove() { ready = false; } @@ -38,14 +37,13 @@ public class PlayerController { if (currentColor == -1) { throw new RuntimeException("Color not found: " + color + "!"); } - currentColor = (currentColor + colors.length -1) % colors.length; + currentColor = (currentColor + colors.length - 1) % colors.length; if (colors[currentColor] == TileColor.NONE) { - currentColor = (currentColor + colors.length -1) % colors.length; + currentColor = (currentColor + colors.length - 1) % colors.length; } color = colors[currentColor]; - setCurrent(false); } - + public void incrementColor() { TileColor[] colors = Board.TileColor.values(); int currentColor = -1; @@ -58,18 +56,13 @@ public class PlayerController { if (currentColor == -1) { throw new RuntimeException("Color not found: " + color + "!"); } - currentColor = (currentColor+1) % colors.length; + currentColor = (currentColor + 1) % colors.length; if (colors[currentColor] == TileColor.NONE) { - currentColor = (currentColor+1) % colors.length; + currentColor = (currentColor + 1) % colors.length; } color = colors[currentColor]; - setCurrent(false); } - - public boolean isCurrent() { - return current; - } - + public boolean isReady() { return ready; } @@ -84,8 +77,4 @@ public class PlayerController { public void setColor(TileColor clr) { color = clr; } - - public void setCurrent(boolean current) { - this.current = current; - } -} +} \ No newline at end of file diff --git a/src/controller/TSPMouseListener.java b/src/controller/TSPMouseListener.java index b11735b..9e645ad 100644 --- a/src/controller/TSPMouseListener.java +++ b/src/controller/TSPMouseListener.java @@ -21,7 +21,7 @@ public class TSPMouseListener implements MouseListener{ @Override public void mouseClicked(MouseEvent arg0) { pc.setColor(tileColor); - tsp.setCurrent(false); + tsp.updateBorders(); } @Override diff --git a/src/model/Referee.java b/src/model/Referee.java index 119c445..5afa161 100644 --- a/src/model/Referee.java +++ b/src/model/Referee.java @@ -1,12 +1,10 @@ package model; -import javax.security.auth.Refreshable; - import model.Board.TileColor; import model.comController.RandomComController; import controller.PlayerController; -public class Referee implements Refreshable { +public class Referee { public enum Message { COM_TURN { @@ -42,7 +40,6 @@ public class Referee implements Refreshable { private final PlayerController pc = new PlayerController(); private boolean playerTurn; - private boolean refresh = true; private int score = 0; public Referee() { @@ -52,28 +49,16 @@ public class Referee implements Refreshable { } public void doSomething() { - Move mv; - if (playerTurn && pc.isReady()) { - mv = pc.getMove(); - + Move mv = pc.getMove(); if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) { playToken(pc.getMove()); - - refresh = true; - } - - else { + } else { pc.denyMove(); } - } - - else if (!playerTurn) { - mv = cc.getMove(); - + } else if (!playerTurn) { + Move mv = cc.getMove(); playToken(mv); - - refresh = true; } } @@ -107,11 +92,6 @@ public class Referee implements Refreshable { return board.getTile(r, c); } - @Override - public boolean isCurrent() { - return !refresh; - } - public boolean isOver() { for (int r = 0; r < Board.NUM_ROWS; r++) { for (int c = 0; c < Board.NUM_COLS; c++) { @@ -135,11 +115,6 @@ public class Referee implements Refreshable { incrementTurn(); } - @Override - public void refresh() { - refresh = false; - } - private void incrementScore() { score++; } diff --git a/src/view/BoardPanel.java b/src/view/BoardPanel.java index 64e6de5..3afe517 100644 --- a/src/view/BoardPanel.java +++ b/src/view/BoardPanel.java @@ -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(); + }); } } diff --git a/src/view/MainFrame.java b/src/view/MainFrame.java index 34e948f..9ae0571 100644 --- a/src/view/MainFrame.java +++ b/src/view/MainFrame.java @@ -3,26 +3,19 @@ package view; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; -import java.util.ArrayList; -import javax.security.auth.RefreshFailedException; -import javax.security.auth.Refreshable; import javax.swing.JFrame; import model.Referee; public class MainFrame extends JFrame { - /** - * - */ + private static final long serialVersionUID = 1L; public static void main(String[] args) { new MainFrame(); } - private final ArrayList elements = new ArrayList(); - private final Referee game; public MainFrame() { @@ -44,16 +37,10 @@ public class MainFrame extends JFrame { private void init() { // Create objects. ScorePanel sp = new ScorePanel(game); - BoardPanel bp = new BoardPanel(game); - MessagePanel mp = new MessagePanel(game); TileSelectionPanel tp = new TileSelectionPanel(game.getPlayer()); - - // Add refreshables. - elements.add(sp); - elements.add(bp); - elements.add(mp); - elements.add(tp); - + BoardPanel bp = new BoardPanel(game,tp); + MessagePanel mp = new MessagePanel(game); + // Add stuff to panel. GridBagLayout layout = new GridBagLayout(); GridBagConstraints con; @@ -100,35 +87,14 @@ public class MainFrame extends JFrame { add(mp); add(bp); add(tp); - } - - private void refresh() { - boolean change = false; - - for (Refreshable r : elements) { - - if (!game.isCurrent() || !r.isCurrent()) { - try { - r.refresh(); - change = true; - } catch (RefreshFailedException e) { - e.printStackTrace(); - } - } - } - - if (change) { - game.refresh(); - repaint(); - pack(); - } + + pack(); + tp.updateBorders(); } private void run() { while (!game.isOver()) { game.doSomething(); - - refresh(); } } -} +} \ No newline at end of file diff --git a/src/view/MessagePanel.java b/src/view/MessagePanel.java index cec2761..c582c43 100644 --- a/src/view/MessagePanel.java +++ b/src/view/MessagePanel.java @@ -1,16 +1,13 @@ package view; -import javax.security.auth.Refreshable; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingUtilities; import model.Referee; -public class MessagePanel extends JPanel implements Refreshable { +public class MessagePanel extends JPanel { - /** - * - */ private static final long serialVersionUID = 1L; private final JLabel message = new JLabel(); @@ -21,13 +18,11 @@ public class MessagePanel extends JPanel implements Refreshable { add(message); } - @Override - public boolean isCurrent() { - return true; - } - - @Override - public void refresh() { - message.setText(referee.getMessage()); + public void updateMessage() { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + message.setText(referee.getMessage()); + } + }); } } diff --git a/src/view/ScorePanel.java b/src/view/ScorePanel.java index ea1d43e..9e0aa8e 100644 --- a/src/view/ScorePanel.java +++ b/src/view/ScorePanel.java @@ -1,16 +1,13 @@ package view; -import javax.security.auth.Refreshable; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingUtilities; import model.Referee; -public class ScorePanel extends JPanel implements Refreshable { +public class ScorePanel extends JPanel { - /** - * - */ private static final long serialVersionUID = 1L; private final JLabel message = new JLabel(); @@ -21,13 +18,11 @@ public class ScorePanel extends JPanel implements Refreshable { add(message); } - @Override - public boolean isCurrent() { - return true; - } - - @Override - public void refresh() { - message.setText("Score: " + referee.getScore()); + public void updateScore() { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + message.setText("Score: " + referee.getScore()); + } + }); } } diff --git a/src/view/TileSelectionPanel.java b/src/view/TileSelectionPanel.java index 7f928ef..51d492c 100644 --- a/src/view/TileSelectionPanel.java +++ b/src/view/TileSelectionPanel.java @@ -5,19 +5,18 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; -import javax.security.auth.RefreshFailedException; -import javax.security.auth.Refreshable; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; +import javax.swing.SwingUtilities; import controller.PlayerController; import controller.TSPMouseListener; import model.Board.TileColor; -public class TileSelectionPanel extends JPanel implements Refreshable { +public class TileSelectionPanel extends JPanel { private static final long serialVersionUID = 1L; @@ -28,7 +27,7 @@ public class TileSelectionPanel extends JPanel implements Refreshable { private final JLabel yellow = new JLabel(new ImageIcon( BoardPanel.YELLOW_ICON)); private final PlayerController pc; - + public TileSelectionPanel(PlayerController pc) { this.pc = pc; @@ -36,40 +35,34 @@ public class TileSelectionPanel extends JPanel implements Refreshable { initActions(); } - @Override - public boolean isCurrent() { - return pc.isCurrent(); - } - - public void setCurrent(boolean current) { - pc.setCurrent(current); - } + public void updateBorders() { + SwingUtilities.invokeLater(new Runnable() { + public void run() { + TileColor tile = pc.getColor(); - @Override - public void refresh() throws RefreshFailedException { - TileColor tile = pc.getColor(); + blue.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + green.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + red.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + yellow.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); - blue.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); - green.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); - red.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); - yellow.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); - - switch (tile) { - case GREEN: - green.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); - break; - case RED: - red.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); - break; - case YELLOW: - yellow.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); - break; - default: - blue.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); - break; - } - - pc.setCurrent(true); + switch (tile) { + case GREEN: + green.setBorder(BorderFactory + .createLineBorder(Color.RED, 3)); + break; + case RED: + red.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); + break; + case YELLOW: + yellow.setBorder(BorderFactory.createLineBorder(Color.RED, + 3)); + break; + default: + blue.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); + break; + } + } + }); } private void initActions() {