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:
Woody Folsom
2012-04-04 17:31:50 -04:00
parent b8df412740
commit e0f42531e7
4 changed files with 35 additions and 13 deletions

View File

@@ -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);

View File

@@ -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
View 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;
}
}