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.
This commit is contained in:
@@ -6,19 +6,15 @@ import java.awt.event.MouseWheelEvent;
|
|||||||
import java.awt.event.MouseWheelListener;
|
import java.awt.event.MouseWheelListener;
|
||||||
|
|
||||||
import view.BoardPanel;
|
import view.BoardPanel;
|
||||||
|
import view.Tile;
|
||||||
import view.TileSelectionPanel;
|
import view.TileSelectionPanel;
|
||||||
|
|
||||||
public class BoardPanelMouseListener implements MouseListener, MouseWheelListener {
|
public class BoardPanelMouseListener implements MouseListener, MouseWheelListener {
|
||||||
|
|
||||||
private final int col;
|
|
||||||
private final int row;
|
|
||||||
private final BoardPanel boardPanel;
|
private final BoardPanel boardPanel;
|
||||||
private final PlayerController pc;
|
private final PlayerController pc;
|
||||||
private final TileSelectionPanel tsp;
|
private final TileSelectionPanel tsp;
|
||||||
|
|
||||||
public BoardPanelMouseListener(BoardPanel boardPanel, TileSelectionPanel tsp, int row, int col, PlayerController pc) {
|
public BoardPanelMouseListener(BoardPanel boardPanel, TileSelectionPanel tsp, PlayerController pc) {
|
||||||
this.row = row;
|
|
||||||
this.col = col;
|
|
||||||
this.pc = pc;
|
this.pc = pc;
|
||||||
this.boardPanel = boardPanel;
|
this.boardPanel = boardPanel;
|
||||||
this.tsp = tsp;
|
this.tsp = tsp;
|
||||||
@@ -26,7 +22,8 @@ public class BoardPanelMouseListener implements MouseListener, MouseWheelListene
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
pc.setCell(row, col);
|
Tile tile = (Tile) e.getComponent();
|
||||||
|
pc.setCell(tile.getRow(), tile.getCol());
|
||||||
boardPanel.updateIcons();
|
boardPanel.updateIcons();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,14 +35,14 @@ public class BoardPanel extends JPanel {
|
|||||||
referee = ref;
|
referee = ref;
|
||||||
setLayout(new GridLayout(Board.NUM_ROWS, Board.NUM_COLS));
|
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 r = 0; r < Board.NUM_ROWS; r++) {
|
||||||
for (int c = 0; c < Board.NUM_COLS; c++) {
|
for (int c = 0; c < Board.NUM_COLS; c++) {
|
||||||
board[r][c] = new JLabel(new ImageIcon(NONE_ICON));
|
Tile tile = new Tile(noneIcon,r,c);
|
||||||
BoardPanelMouseListener bpml = new BoardPanelMouseListener(this, tsp, r,
|
board[r][c] = tile;
|
||||||
c, referee.getPlayer());
|
tile.addMouseListener(bpml);
|
||||||
|
|
||||||
// separate listener on each JLabel? This should probably be
|
|
||||||
// changed.
|
|
||||||
board[r][c].addMouseListener(bpml);
|
board[r][c].addMouseListener(bpml);
|
||||||
board[r][c].addMouseWheelListener(bpml);
|
board[r][c].addMouseWheelListener(bpml);
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public class MainFrame extends JFrame {
|
|||||||
private final Referee game;
|
private final Referee game;
|
||||||
|
|
||||||
public MainFrame() {
|
public MainFrame() {
|
||||||
|
super("Project 4");
|
||||||
game = new Referee();
|
game = new Referee();
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|||||||
24
src/view/Tile.java
Normal file
24
src/view/Tile.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user