From 0f616a3cc6121ebaebb22ab332805dbc97f208d3 Mon Sep 17 00:00:00 2001 From: Woody Folsom Date: Wed, 4 Apr 2012 15:31:27 -0400 Subject: [PATCH] Continuing gradual process of collecting state from various GUI components and handlers and aggregating it in PlayerController. Removed references to Refree from some handlers, replacing them with direct use of PlayerController (Law of Demeter). --- src/controller/BoardPanelMouseListener.java | 54 +++++++++ src/controller/PlayerController.java | 55 ++++++++- src/controller/TSPMouseListener.java | 42 +++++++ src/view/BoardPanel.java | 46 ++------ src/view/MainFrame.java | 2 +- src/view/TileSelectionPanel.java | 124 +++----------------- 6 files changed, 176 insertions(+), 147 deletions(-) create mode 100644 src/controller/BoardPanelMouseListener.java create mode 100644 src/controller/TSPMouseListener.java diff --git a/src/controller/BoardPanelMouseListener.java b/src/controller/BoardPanelMouseListener.java new file mode 100644 index 0000000..58b7ecc --- /dev/null +++ b/src/controller/BoardPanelMouseListener.java @@ -0,0 +1,54 @@ +package controller; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; + +public class BoardPanelMouseListener implements MouseListener, MouseWheelListener { + + private final int col; + private final int row; + private final PlayerController pc; + + public BoardPanelMouseListener(int row, int col, PlayerController pc) { + this.row = row; + this.col = col; + this.pc = pc; + } + + @Override + public void mouseClicked(MouseEvent e) { + pc.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. + } + + @Override + public void mouseWheelMoved(MouseWheelEvent e) { + if (e.getWheelRotation() > 0) { + pc.incrementColor(); + } else if (e.getWheelRotation() < 0) { + pc.decrementColor(); + } + } + +} \ No newline at end of file diff --git a/src/controller/PlayerController.java b/src/controller/PlayerController.java index 78f51c3..506f972 100644 --- a/src/controller/PlayerController.java +++ b/src/controller/PlayerController.java @@ -1,14 +1,17 @@ package controller; import model.Board.TileColor; +import model.Board; import model.CellPointer; import model.Move; public class PlayerController { private final CellPointer cell = new CellPointer(0, 0); - private TileColor color = TileColor.BLUE; + + private boolean current = true; private boolean ready = false; - + private TileColor color = TileColor.BLUE; + public void denyMove() { ready = false; } @@ -23,6 +26,50 @@ public class PlayerController { } + public void decrementColor() { + TileColor[] colors = Board.TileColor.values(); + int currentColor = -1; + for (int i = 0; i < colors.length; i++) { + if (colors[i] == color) { + currentColor = i; + break; + } + } + if (currentColor == -1) { + throw new RuntimeException("Color not found: " + color + "!"); + } + currentColor = (currentColor + colors.length -1) % colors.length; + if (colors[currentColor] == TileColor.NONE) { + currentColor = (currentColor + colors.length -1) % colors.length; + } + color = colors[currentColor]; + setCurrent(false); + } + + public void incrementColor() { + TileColor[] colors = Board.TileColor.values(); + int currentColor = -1; + for (int i = 0; i < colors.length; i++) { + if (colors[i] == color) { + currentColor = i; + break; + } + } + if (currentColor == -1) { + throw new RuntimeException("Color not found: " + color + "!"); + } + currentColor = (currentColor+1) % colors.length; + if (colors[currentColor] == TileColor.NONE) { + currentColor = (currentColor+1) % colors.length; + } + color = colors[currentColor]; + setCurrent(false); + } + + public boolean isCurrent() { + return current; + } + public boolean isReady() { return ready; } @@ -37,4 +84,8 @@ public class PlayerController { public void setColor(TileColor clr) { color = clr; } + + public void setCurrent(boolean current) { + this.current = current; + } } diff --git a/src/controller/TSPMouseListener.java b/src/controller/TSPMouseListener.java new file mode 100644 index 0000000..b11735b --- /dev/null +++ b/src/controller/TSPMouseListener.java @@ -0,0 +1,42 @@ +package controller; + +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +import view.TileSelectionPanel; + +import model.Board.TileColor; + +public class TSPMouseListener implements MouseListener{ + private PlayerController pc; + private TileColor tileColor; + private TileSelectionPanel tsp; + + public TSPMouseListener(PlayerController pc, TileSelectionPanel tsp, TileColor tileColor) { + this.pc = pc; + this.tileColor = tileColor; + this.tsp = tsp; + } + + @Override + public void mouseClicked(MouseEvent arg0) { + pc.setColor(tileColor); + tsp.setCurrent(false); + } + + @Override + public void mouseEntered(MouseEvent arg0) { + } + + @Override + public void mouseExited(MouseEvent arg0) { + } + + @Override + public void mousePressed(MouseEvent arg0) { + } + + @Override + public void mouseReleased(MouseEvent arg0) { + } +} \ No newline at end of file diff --git a/src/view/BoardPanel.java b/src/view/BoardPanel.java index 0ed960c..64e6de5 100644 --- a/src/view/BoardPanel.java +++ b/src/view/BoardPanel.java @@ -1,8 +1,6 @@ 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; @@ -10,46 +8,13 @@ import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; +import controller.BoardPanelMouseListener; + 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 = BoardPanel.class .getResource("/img/blue.png"); @@ -74,7 +39,12 @@ public class BoardPanel extends JPanel implements Refreshable { 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)); + BoardPanelMouseListener bpml = new BoardPanelMouseListener(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]); } } diff --git a/src/view/MainFrame.java b/src/view/MainFrame.java index 3aac3cf..34e948f 100644 --- a/src/view/MainFrame.java +++ b/src/view/MainFrame.java @@ -46,7 +46,7 @@ public class MainFrame extends JFrame { ScorePanel sp = new ScorePanel(game); BoardPanel bp = new BoardPanel(game); MessagePanel mp = new MessagePanel(game); - TileSelectionPanel tp = new TileSelectionPanel(game); + TileSelectionPanel tp = new TileSelectionPanel(game.getPlayer()); // Add refreshables. elements.add(sp); diff --git a/src/view/TileSelectionPanel.java b/src/view/TileSelectionPanel.java index 1be432d..7f928ef 100644 --- a/src/view/TileSelectionPanel.java +++ b/src/view/TileSelectionPanel.java @@ -4,8 +4,6 @@ import java.awt.Color; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; import javax.security.auth.RefreshFailedException; import javax.security.auth.Refreshable; @@ -14,27 +12,25 @@ import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JPanel; +import controller.PlayerController; +import controller.TSPMouseListener; + import model.Board.TileColor; -import model.Referee; public class TileSelectionPanel extends JPanel implements Refreshable { - /** - * - */ private static final long serialVersionUID = 1L; private final JLabel blue = new JLabel(new ImageIcon(BoardPanel.BLUE_ICON)); private final JLabel green = new JLabel( new ImageIcon(BoardPanel.GREEN_ICON)); - private boolean isCurrent = true; private final JLabel red = new JLabel(new ImageIcon(BoardPanel.RED_ICON)); - private final Referee referee; private final JLabel yellow = new JLabel(new ImageIcon( BoardPanel.YELLOW_ICON)); - - public TileSelectionPanel(Referee ref) { - referee = ref; + private final PlayerController pc; + + public TileSelectionPanel(PlayerController pc) { + this.pc = pc; initLayout(); initActions(); @@ -42,12 +38,16 @@ public class TileSelectionPanel extends JPanel implements Refreshable { @Override public boolean isCurrent() { - return isCurrent; + return pc.isCurrent(); + } + + public void setCurrent(boolean current) { + pc.setCurrent(current); } @Override public void refresh() throws RefreshFailedException { - TileColor tile = referee.getPlayer().getColor(); + TileColor tile = pc.getColor(); blue.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); green.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); @@ -69,102 +69,14 @@ public class TileSelectionPanel extends JPanel implements Refreshable { break; } - isCurrent = true; + pc.setCurrent(true); } private void initActions() { - blue.addMouseListener(new MouseListener() { - @Override - public void mouseClicked(MouseEvent arg0) { - referee.getPlayer().setColor(TileColor.BLUE); - isCurrent = false; - } - - @Override - public void mouseEntered(MouseEvent arg0) { - } - - @Override - public void mouseExited(MouseEvent arg0) { - } - - @Override - public void mousePressed(MouseEvent arg0) { - } - - @Override - public void mouseReleased(MouseEvent arg0) { - } - }); - green.addMouseListener(new MouseListener() { - @Override - public void mouseClicked(MouseEvent arg0) { - referee.getPlayer().setColor(TileColor.GREEN); - isCurrent = false; - } - - @Override - public void mouseEntered(MouseEvent arg0) { - } - - @Override - public void mouseExited(MouseEvent arg0) { - } - - @Override - public void mousePressed(MouseEvent arg0) { - } - - @Override - public void mouseReleased(MouseEvent arg0) { - } - }); - red.addMouseListener(new MouseListener() { - @Override - public void mouseClicked(MouseEvent arg0) { - referee.getPlayer().setColor(TileColor.RED); - isCurrent = false; - } - - @Override - public void mouseEntered(MouseEvent arg0) { - } - - @Override - public void mouseExited(MouseEvent arg0) { - } - - @Override - public void mousePressed(MouseEvent arg0) { - } - - @Override - public void mouseReleased(MouseEvent arg0) { - } - }); - yellow.addMouseListener(new MouseListener() { - @Override - public void mouseClicked(MouseEvent arg0) { - referee.getPlayer().setColor(TileColor.YELLOW); - isCurrent = false; - } - - @Override - public void mouseEntered(MouseEvent arg0) { - } - - @Override - public void mouseExited(MouseEvent arg0) { - } - - @Override - public void mousePressed(MouseEvent arg0) { - } - - @Override - public void mouseReleased(MouseEvent arg0) { - } - }); + blue.addMouseListener(new TSPMouseListener(pc, this, TileColor.BLUE)); + green.addMouseListener(new TSPMouseListener(pc, this, TileColor.GREEN)); + red.addMouseListener(new TSPMouseListener(pc, this, TileColor.RED)); + yellow.addMouseListener(new TSPMouseListener(pc, this, TileColor.YELLOW)); } private void initLayout() {