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 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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ public class MainFrame extends JFrame {
|
||||
private final Referee game;
|
||||
|
||||
public MainFrame() {
|
||||
super("Project 4");
|
||||
game = new Referee();
|
||||
|
||||
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