From e0f42531e758263c5bcdc46ccc6bd0b550948115 Mon Sep 17 00:00:00 2001 From: Woody Folsom Date: Wed, 4 Apr 2012 17:31:50 -0400 Subject: [PATCH] Replaced having 1 MouseListener per Tile JLabel which a single listener which asks the event's associated component for its row, column. This isn't ideal, but is preferable to putting this information in each of rows x columns different mouse listeners. Also added a title to the MainFrame. --- src/controller/BoardPanelMouseListener.java | 11 ++++------ src/view/BoardPanel.java | 12 +++++------ src/view/MainFrame.java | 1 + src/view/Tile.java | 24 +++++++++++++++++++++ 4 files changed, 35 insertions(+), 13 deletions(-) create mode 100644 src/view/Tile.java diff --git a/src/controller/BoardPanelMouseListener.java b/src/controller/BoardPanelMouseListener.java index 741c5a0..cb64417 100644 --- a/src/controller/BoardPanelMouseListener.java +++ b/src/controller/BoardPanelMouseListener.java @@ -6,19 +6,15 @@ import java.awt.event.MouseWheelEvent; import java.awt.event.MouseWheelListener; import view.BoardPanel; +import view.Tile; 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(BoardPanel boardPanel, TileSelectionPanel tsp, int row, int col, PlayerController pc) { - this.row = row; - this.col = col; + public BoardPanelMouseListener(BoardPanel boardPanel, TileSelectionPanel tsp, PlayerController pc) { this.pc = pc; this.boardPanel = boardPanel; this.tsp = tsp; @@ -26,7 +22,8 @@ public class BoardPanelMouseListener implements MouseListener, MouseWheelListene @Override public void mouseClicked(MouseEvent e) { - pc.setCell(row, col); + Tile tile = (Tile) e.getComponent(); + pc.setCell(tile.getRow(), tile.getCol()); boardPanel.updateIcons(); } diff --git a/src/view/BoardPanel.java b/src/view/BoardPanel.java index 3afe517..843f6fb 100644 --- a/src/view/BoardPanel.java +++ b/src/view/BoardPanel.java @@ -35,14 +35,14 @@ public class BoardPanel extends JPanel { referee = ref; setLayout(new GridLayout(Board.NUM_ROWS, Board.NUM_COLS)); + BoardPanelMouseListener bpml = new BoardPanelMouseListener(this, tsp, referee.getPlayer()); + ImageIcon noneIcon = new ImageIcon(NONE_ICON); 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(this, tsp, r, - c, referee.getPlayer()); - - // separate listener on each JLabel? This should probably be - // changed. + Tile tile = new Tile(noneIcon,r,c); + board[r][c] = tile; + tile.addMouseListener(bpml); + board[r][c].addMouseListener(bpml); board[r][c].addMouseWheelListener(bpml); diff --git a/src/view/MainFrame.java b/src/view/MainFrame.java index 9ae0571..141f8ac 100644 --- a/src/view/MainFrame.java +++ b/src/view/MainFrame.java @@ -19,6 +19,7 @@ public class MainFrame extends JFrame { private final Referee game; public MainFrame() { + super("Project 4"); game = new Referee(); init(); diff --git a/src/view/Tile.java b/src/view/Tile.java new file mode 100644 index 0000000..f06e315 --- /dev/null +++ b/src/view/Tile.java @@ -0,0 +1,24 @@ +package view; + +import javax.swing.ImageIcon; +import javax.swing.JLabel; + +public class Tile extends JLabel { + private static final long serialVersionUID = 1L; + private final int r; + private final int c; + + public Tile(ImageIcon imgIcon, int r, int c) { + super(imgIcon); + this.r = r; + this.c = c; + } + + public int getCol() { + return c; + } + + public int getRow() { + return r; + } +}