diff --git a/bin/controller/PlayerController.class b/bin/controller/PlayerController.class new file mode 100644 index 0000000..56e2858 Binary files /dev/null and b/bin/controller/PlayerController.class differ diff --git a/bin/img/blue.png b/bin/img/blue.png new file mode 100644 index 0000000..f1eb446 Binary files /dev/null and b/bin/img/blue.png differ diff --git a/bin/img/green.png b/bin/img/green.png new file mode 100644 index 0000000..fe1b4fb Binary files /dev/null and b/bin/img/green.png differ diff --git a/bin/img/none.png b/bin/img/none.png new file mode 100644 index 0000000..482d3ad Binary files /dev/null and b/bin/img/none.png differ diff --git a/bin/img/red.png b/bin/img/red.png new file mode 100644 index 0000000..54f8e4f Binary files /dev/null and b/bin/img/red.png differ diff --git a/bin/img/yellow.png b/bin/img/yellow.png new file mode 100644 index 0000000..7ed3687 Binary files /dev/null and b/bin/img/yellow.png differ diff --git a/bin/model/Board$TileColor.class b/bin/model/Board$TileColor.class new file mode 100644 index 0000000..01bc7bd Binary files /dev/null and b/bin/model/Board$TileColor.class differ diff --git a/bin/model/Board.class b/bin/model/Board.class new file mode 100644 index 0000000..cea2a67 Binary files /dev/null and b/bin/model/Board.class differ diff --git a/bin/model/CellPointer.class b/bin/model/CellPointer.class new file mode 100644 index 0000000..5c54f1a Binary files /dev/null and b/bin/model/CellPointer.class differ diff --git a/bin/model/ComController.class b/bin/model/ComController.class new file mode 100644 index 0000000..f01eda7 Binary files /dev/null and b/bin/model/ComController.class differ diff --git a/bin/model/Move.class b/bin/model/Move.class new file mode 100644 index 0000000..169f9e5 Binary files /dev/null and b/bin/model/Move.class differ diff --git a/bin/model/Referee$Message$1.class b/bin/model/Referee$Message$1.class new file mode 100644 index 0000000..1b58e3b Binary files /dev/null and b/bin/model/Referee$Message$1.class differ diff --git a/bin/model/Referee$Message$2.class b/bin/model/Referee$Message$2.class new file mode 100644 index 0000000..bbf633a Binary files /dev/null and b/bin/model/Referee$Message$2.class differ diff --git a/bin/model/Referee$Message$3.class b/bin/model/Referee$Message$3.class new file mode 100644 index 0000000..dbcf49b Binary files /dev/null and b/bin/model/Referee$Message$3.class differ diff --git a/bin/model/Referee$Message$4.class b/bin/model/Referee$Message$4.class new file mode 100644 index 0000000..1d2b460 Binary files /dev/null and b/bin/model/Referee$Message$4.class differ diff --git a/bin/model/Referee$Message.class b/bin/model/Referee$Message.class new file mode 100644 index 0000000..42e39c9 Binary files /dev/null and b/bin/model/Referee$Message.class differ diff --git a/bin/model/Referee.class b/bin/model/Referee.class new file mode 100644 index 0000000..07caa9f Binary files /dev/null and b/bin/model/Referee.class differ diff --git a/bin/model/comController/RandomComController.class b/bin/model/comController/RandomComController.class new file mode 100644 index 0000000..936c99c Binary files /dev/null and b/bin/model/comController/RandomComController.class differ diff --git a/bin/view/BoardPanel$BoardPanelMouseListener.class b/bin/view/BoardPanel$BoardPanelMouseListener.class new file mode 100644 index 0000000..7a35d03 Binary files /dev/null and b/bin/view/BoardPanel$BoardPanelMouseListener.class differ diff --git a/bin/view/BoardPanel.class b/bin/view/BoardPanel.class new file mode 100644 index 0000000..5e509b2 Binary files /dev/null and b/bin/view/BoardPanel.class differ diff --git a/bin/view/MainFrame.class b/bin/view/MainFrame.class new file mode 100644 index 0000000..804ea2f Binary files /dev/null and b/bin/view/MainFrame.class differ diff --git a/bin/view/MessagePanel.class b/bin/view/MessagePanel.class new file mode 100644 index 0000000..bea67c5 Binary files /dev/null and b/bin/view/MessagePanel.class differ diff --git a/bin/view/ScorePanel.class b/bin/view/ScorePanel.class new file mode 100644 index 0000000..91b8f44 Binary files /dev/null and b/bin/view/ScorePanel.class differ diff --git a/bin/view/TileSelectionPanel$1.class b/bin/view/TileSelectionPanel$1.class new file mode 100644 index 0000000..4b449c9 Binary files /dev/null and b/bin/view/TileSelectionPanel$1.class differ diff --git a/bin/view/TileSelectionPanel$2.class b/bin/view/TileSelectionPanel$2.class new file mode 100644 index 0000000..97c1c17 Binary files /dev/null and b/bin/view/TileSelectionPanel$2.class differ diff --git a/bin/view/TileSelectionPanel$3.class b/bin/view/TileSelectionPanel$3.class new file mode 100644 index 0000000..1f7ed42 Binary files /dev/null and b/bin/view/TileSelectionPanel$3.class differ diff --git a/bin/view/TileSelectionPanel$4.class b/bin/view/TileSelectionPanel$4.class new file mode 100644 index 0000000..fe89236 Binary files /dev/null and b/bin/view/TileSelectionPanel$4.class differ diff --git a/bin/view/TileSelectionPanel.class b/bin/view/TileSelectionPanel.class new file mode 100644 index 0000000..9013e9e Binary files /dev/null and b/bin/view/TileSelectionPanel.class differ diff --git a/src/controller/PlayerController.java b/src/controller/PlayerController.java new file mode 100644 index 0000000..78f51c3 --- /dev/null +++ b/src/controller/PlayerController.java @@ -0,0 +1,40 @@ +package controller; + +import model.Board.TileColor; +import model.CellPointer; +import model.Move; + +public class PlayerController { + private final CellPointer cell = new CellPointer(0, 0); + private TileColor color = TileColor.BLUE; + private boolean ready = false; + + public void denyMove() { + ready = false; + } + + public TileColor getColor() { + return color; + } + + public Move getMove() { + ready = false; + return new Move(cell, color); + + } + + public boolean isReady() { + return ready; + } + + public void setCell(int row, int col) { + cell.r = row; + cell.c = col; + + ready = true; + } + + public void setColor(TileColor clr) { + color = clr; + } +} diff --git a/src/img/blue.png b/src/img/blue.png new file mode 100644 index 0000000..f1eb446 Binary files /dev/null and b/src/img/blue.png differ diff --git a/src/img/green.png b/src/img/green.png new file mode 100644 index 0000000..fe1b4fb Binary files /dev/null and b/src/img/green.png differ diff --git a/src/img/none.png b/src/img/none.png new file mode 100644 index 0000000..482d3ad Binary files /dev/null and b/src/img/none.png differ diff --git a/src/img/red.png b/src/img/red.png new file mode 100644 index 0000000..54f8e4f Binary files /dev/null and b/src/img/red.png differ diff --git a/src/img/yellow.png b/src/img/yellow.png new file mode 100644 index 0000000..7ed3687 Binary files /dev/null and b/src/img/yellow.png differ diff --git a/src/model/Board.java b/src/model/Board.java new file mode 100644 index 0000000..df143db --- /dev/null +++ b/src/model/Board.java @@ -0,0 +1,163 @@ +package model; + +import java.util.ArrayList; + +public class Board { + public enum TileColor { + BLUE, GREEN, NONE, RED, YELLOW + } + + public static final int NUM_COLS = 6; + public static final int NUM_ROWS = 6; + public static final int ROW_REMOVAL_SIZE = 3; + + private final TileColor[][] board; + + public Board() { + board = new TileColor[NUM_ROWS][NUM_COLS]; + for (int r = 0; r < NUM_ROWS; r++) { + for (int c = 0; c < NUM_COLS; c++) { + setTile(new CellPointer(r, c), TileColor.NONE); + } + } + } + + public TileColor getTile(int r, int c) { + return board[r][c]; + } + + public void setTile(CellPointer cp, TileColor tile) { + board[cp.r][cp.c] = tile; + + if (tile != TileColor.NONE) { + ArrayList remove = new ArrayList(); + ArrayList hold; + int count; + + // Check up-and-down. + count = 0; + hold = new ArrayList(); + loop: for (int row = cp.r; row >= 0; row--) { + if (board[row][cp.c] == tile) { + hold.add(new CellPointer(row, cp.c)); + count++; + } else { + break loop; + } + } + + loop: for (int row = cp.r + 1; row < NUM_ROWS; row++) { + if (board[row][cp.c] == tile) { + hold.add(new CellPointer(row, cp.c)); + count++; + } else { + break loop; + } + } + + if (count >= ROW_REMOVAL_SIZE) { + remove.addAll(hold); + } + + // Check left-and-right. + count = 0; + hold = new ArrayList(); + loop: for (int col = cp.c; col >= 0; col--) { + if (board[cp.r][col] == tile) { + hold.add(new CellPointer(cp.r, col)); + count++; + } else { + break loop; + } + } + + loop: for (int col = cp.c + 1; col < NUM_COLS; col++) { + if (board[cp.r][col] == tile) { + hold.add(new CellPointer(cp.r, col)); + count++; + } else { + break loop; + } + } + + if (count >= ROW_REMOVAL_SIZE) { + remove.addAll(hold); + } + + // Check descending diagonal. + count = 0; + hold = new ArrayList(); + int row = cp.r; + int col = cp.c; + loop: while (row >= 0 && col >= 0) { + if (board[row][col] == tile) { + hold.add(new CellPointer(row, col)); + count++; + } else { + break loop; + } + + row--; + col--; + } + + row = cp.r + 1; + col = cp.c + 1; + loop: while (row < NUM_ROWS && col < NUM_COLS) { + if (board[row][col] == tile) { + hold.add(new CellPointer(row, col)); + count++; + } else { + break loop; + } + + row++; + col++; + } + + if (count >= ROW_REMOVAL_SIZE) { + remove.addAll(hold); + } + + // Check ascending diagonal. + count = 0; + hold = new ArrayList(); + + row = cp.r; + col = cp.c; + loop: while (row < NUM_ROWS && col >= 0) { + if (board[row][col] == tile) { + hold.add(new CellPointer(row, col)); + count++; + } else { + break loop; + } + + row++; + col--; + } + + row = cp.r - 1; + col = cp.c + 1; + loop: while (row >= 0 && col < NUM_COLS) { + if (board[row][col] == tile) { + hold.add(new CellPointer(row, col)); + count++; + } else { + break loop; + } + + row--; + col++; + } + + if (count >= ROW_REMOVAL_SIZE) { + remove.addAll(hold); + } + + for (CellPointer cell : remove) { + board[cell.r][cell.c] = TileColor.NONE; + } + } + } +} diff --git a/src/model/CellPointer.java b/src/model/CellPointer.java new file mode 100644 index 0000000..8ffd57c --- /dev/null +++ b/src/model/CellPointer.java @@ -0,0 +1,11 @@ +package model; + +public class CellPointer { + public CellPointer(int row, int col) { + r = row; + c = col; + } + + public int r; + public int c; +} diff --git a/src/model/ComController.java b/src/model/ComController.java new file mode 100644 index 0000000..c64ce00 --- /dev/null +++ b/src/model/ComController.java @@ -0,0 +1,13 @@ +package model; + + +public abstract class ComController { + + protected Board board = null; + + public ComController(Board brd) { + board = brd; + } + + public abstract Move getMove(); +} diff --git a/src/model/Move.java b/src/model/Move.java new file mode 100644 index 0000000..db586d6 --- /dev/null +++ b/src/model/Move.java @@ -0,0 +1,21 @@ +package model; + +import model.Board.TileColor; + +public class Move { + private final TileColor color; + private final CellPointer cp; + + public Move(CellPointer cllPntr, TileColor tlClr) { + cp = cllPntr; + color = tlClr; + } + + public CellPointer getCell() { + return cp; + } + + public TileColor getColor() { + return color; + } +} diff --git a/src/model/Referee.java b/src/model/Referee.java new file mode 100644 index 0000000..119c445 --- /dev/null +++ b/src/model/Referee.java @@ -0,0 +1,154 @@ +package model; + +import javax.security.auth.Refreshable; + +import model.Board.TileColor; +import model.comController.RandomComController; +import controller.PlayerController; + +public class Referee implements Refreshable { + + public enum Message { + COM_TURN { + @Override + public String toString() { + return "Waiting for the computer's move."; + } + }, + GAME_OVER { + @Override + public String toString() { + return "Game over!"; + } + }, + NONE { + @Override + public String toString() { + return ""; + } + }, + PLAYER_TURN { + @Override + public String toString() { + return "Waiting for the player's move."; + } + } + } + + private final Board board; + + private final ComController cc; + + private final PlayerController pc = new PlayerController(); + + private boolean playerTurn; + private boolean refresh = true; + private int score = 0; + + public Referee() { + board = new Board(); + cc = new RandomComController(board); + playerTurn = true; + } + + public void doSomething() { + Move mv; + + if (playerTurn && pc.isReady()) { + mv = pc.getMove(); + + if (board.getTile(mv.getCell().r, mv.getCell().c) == TileColor.NONE) { + playToken(pc.getMove()); + + refresh = true; + } + + else { + pc.denyMove(); + } + } + + else if (!playerTurn) { + mv = cc.getMove(); + + playToken(mv); + + refresh = true; + } + } + + public ComController getCom() { + return cc; + } + + public String getMessage() { + if (isOver()) { + return Message.GAME_OVER.toString(); + } + + else if (isPlayersTurn()) { + return Message.PLAYER_TURN.toString(); + } + + else { + return Message.COM_TURN.toString(); + } + } + + public PlayerController getPlayer() { + return pc; + } + + public int getScore() { + return score; + } + + public TileColor getTile(int r, int c) { + return board.getTile(r, c); + } + + @Override + public boolean isCurrent() { + return !refresh; + } + + public boolean isOver() { + for (int r = 0; r < Board.NUM_ROWS; r++) { + for (int c = 0; c < Board.NUM_COLS; c++) { + if (board.getTile(r, c) == TileColor.NONE) { + return false; + } + } + } + + return true; + } + + public void playToken(Move move) { + + board.setTile(move.getCell(), move.getColor()); + + if (playerTurn) { + incrementScore(); + } + + incrementTurn(); + } + + @Override + public void refresh() { + refresh = false; + } + + private void incrementScore() { + score++; + } + + private void incrementTurn() { + playerTurn = !playerTurn; + } + + private boolean isPlayersTurn() { + return playerTurn; + } +} diff --git a/src/model/comController/RandomComController.java b/src/model/comController/RandomComController.java new file mode 100644 index 0000000..b85a472 --- /dev/null +++ b/src/model/comController/RandomComController.java @@ -0,0 +1,48 @@ +package model.comController; + +import java.util.Random; + +import model.Board; +import model.Board.TileColor; +import model.CellPointer; +import model.ComController; +import model.Move; + +public class RandomComController extends ComController { + + private final Random rand = new Random(); + + public RandomComController(Board brd) { + super(brd); + } + + @Override + public Move getMove() { + TileColor tile = TileColor.BLUE; + int r = -1; + int c = -1; + + while (tile != TileColor.NONE) { + r = rand.nextInt(Board.NUM_ROWS); + c = rand.nextInt(Board.NUM_COLS); + + tile = board.getTile(r, c); + } + + switch (rand.nextInt(4)) { + case 0: + tile = TileColor.BLUE; + break; + case 1: + tile = TileColor.GREEN; + break; + case 2: + tile = TileColor.RED; + break; + case 3: + tile = TileColor.YELLOW; + } + + return new Move(new CellPointer(r, c), tile); + } +} diff --git a/src/view/BoardPanel.java b/src/view/BoardPanel.java new file mode 100644 index 0000000..0a7eadd --- /dev/null +++ b/src/view/BoardPanel.java @@ -0,0 +1,120 @@ +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; +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.JPanel; + +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 = ClassLoader + .getSystemResource("img/blue.png"); + + public static final URL GREEN_ICON = ClassLoader + .getSystemResource("img/green.png"); + public static final URL NONE_ICON = ClassLoader + .getSystemResource("img/none.png"); + public static final URL RED_ICON = ClassLoader + .getSystemResource("img/red.png"); + public static final URL YELLOW_ICON = ClassLoader + .getSystemResource("img/yellow.png"); + /** + * + */ + private static final long serialVersionUID = 1L; + + private final JLabel[][] board = new JLabel[Board.NUM_ROWS][Board.NUM_COLS]; + private final Referee referee; + + // private final ImageIcon[][] icons = new + // ImageIcon[Board.NUM_ROWS][Board.NUM_COLS]; + + public BoardPanel(Referee ref) { + referee = ref; + setLayout(new GridLayout(Board.NUM_ROWS, Board.NUM_COLS)); + + 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)); + add(board[r][c]); + } + } + + refresh(); + } + + @Override + public boolean isCurrent() { + return true; + } + + @Override + public void refresh() { + for (int r = 0; r < Board.NUM_ROWS; r++) { + for (int c = 0; c < Board.NUM_COLS; c++) { + switch (referee.getTile(r, c)) { + case BLUE: + board[r][c].setIcon(new ImageIcon(BLUE_ICON)); + break; + case GREEN: + board[r][c].setIcon(new ImageIcon(GREEN_ICON)); + break; + case RED: + board[r][c].setIcon(new ImageIcon(RED_ICON)); + break; + case YELLOW: + board[r][c].setIcon(new ImageIcon(YELLOW_ICON)); + break; + default: + board[r][c].setIcon(new ImageIcon(NONE_ICON)); + } + } + } + repaint(); + } +} diff --git a/src/view/MainFrame.java b/src/view/MainFrame.java new file mode 100644 index 0000000..3aac3cf --- /dev/null +++ b/src/view/MainFrame.java @@ -0,0 +1,134 @@ +package view; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.util.ArrayList; + +import javax.security.auth.RefreshFailedException; +import javax.security.auth.Refreshable; +import javax.swing.JFrame; + +import model.Referee; + +public class MainFrame extends JFrame { + /** + * + */ + private static final long serialVersionUID = 1L; + + public static void main(String[] args) { + new MainFrame(); + } + + private final ArrayList elements = new ArrayList(); + + private final Referee game; + + public MainFrame() { + game = new Referee(); + + init(); + + // Set up window. + setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE); + setResizable(false); + + pack(); + setLocationRelativeTo(null); + setVisible(true); + + run(); + } + + private void init() { + // Create objects. + ScorePanel sp = new ScorePanel(game); + BoardPanel bp = new BoardPanel(game); + MessagePanel mp = new MessagePanel(game); + TileSelectionPanel tp = new TileSelectionPanel(game); + + // Add refreshables. + elements.add(sp); + elements.add(bp); + elements.add(mp); + elements.add(tp); + + // Add stuff to panel. + GridBagLayout layout = new GridBagLayout(); + GridBagConstraints con; + + con = new GridBagConstraints(); + con.anchor = GridBagConstraints.WEST; + con.fill = GridBagConstraints.BOTH; + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 0; + con.gridy = 0; + con.weightx = 1; + layout.setConstraints(sp, con); + + con = new GridBagConstraints(); + con.anchor = GridBagConstraints.EAST; + con.fill = GridBagConstraints.BOTH; + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 1; + con.gridy = 0; + con.weightx = 1; + layout.setConstraints(mp, con); + + con = new GridBagConstraints(); + con.anchor = GridBagConstraints.NORTH; + con.fill = GridBagConstraints.BOTH; + con.gridwidth = 2; + con.gridx = 0; + con.gridy = 1; + layout.setConstraints(bp, con); + + con = new GridBagConstraints(); + con.anchor = GridBagConstraints.NORTH; + con.fill = GridBagConstraints.BOTH; + con.gridwidth = 2; + con.gridx = 0; + con.gridy = 2; + con.insets = new Insets(10, 0, 10, 0); + layout.setConstraints(tp, con); + + setLayout(layout); + add(sp); + add(mp); + add(bp); + add(tp); + } + + private void refresh() { + boolean change = false; + + for (Refreshable r : elements) { + + if (!game.isCurrent() || !r.isCurrent()) { + try { + r.refresh(); + change = true; + } catch (RefreshFailedException e) { + e.printStackTrace(); + } + } + } + + if (change) { + game.refresh(); + repaint(); + pack(); + } + } + + private void run() { + while (!game.isOver()) { + game.doSomething(); + + refresh(); + } + } +} diff --git a/src/view/MessagePanel.java b/src/view/MessagePanel.java new file mode 100644 index 0000000..cec2761 --- /dev/null +++ b/src/view/MessagePanel.java @@ -0,0 +1,33 @@ +package view; + +import javax.security.auth.Refreshable; +import javax.swing.JLabel; +import javax.swing.JPanel; + +import model.Referee; + +public class MessagePanel extends JPanel implements Refreshable { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private final JLabel message = new JLabel(); + private final Referee referee; + + public MessagePanel(Referee ref) { + referee = ref; + add(message); + } + + @Override + public boolean isCurrent() { + return true; + } + + @Override + public void refresh() { + message.setText(referee.getMessage()); + } +} diff --git a/src/view/ScorePanel.java b/src/view/ScorePanel.java new file mode 100644 index 0000000..ea1d43e --- /dev/null +++ b/src/view/ScorePanel.java @@ -0,0 +1,33 @@ +package view; + +import javax.security.auth.Refreshable; +import javax.swing.JLabel; +import javax.swing.JPanel; + +import model.Referee; + +public class ScorePanel extends JPanel implements Refreshable { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private final JLabel message = new JLabel(); + private final Referee referee; + + public ScorePanel(Referee ref) { + referee = ref; + add(message); + } + + @Override + public boolean isCurrent() { + return true; + } + + @Override + public void refresh() { + message.setText("Score: " + referee.getScore()); + } +} diff --git a/src/view/TileSelectionPanel.java b/src/view/TileSelectionPanel.java new file mode 100644 index 0000000..1be432d --- /dev/null +++ b/src/view/TileSelectionPanel.java @@ -0,0 +1,236 @@ +package view; + +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; +import javax.swing.BorderFactory; +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.JPanel; + +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; + + initLayout(); + initActions(); + } + + @Override + public boolean isCurrent() { + return isCurrent; + } + + @Override + public void refresh() throws RefreshFailedException { + TileColor tile = referee.getPlayer().getColor(); + + blue.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + green.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + red.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + yellow.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); + + switch (tile) { + case GREEN: + green.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); + break; + case RED: + red.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); + break; + case YELLOW: + yellow.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); + break; + default: + blue.setBorder(BorderFactory.createLineBorder(Color.RED, 3)); + break; + } + + isCurrent = 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) { + } + }); + } + + private void initLayout() { + GridBagLayout layout = new GridBagLayout(); + GridBagConstraints con; + JLabel spacer1 = new JLabel(); + JLabel spacer2 = new JLabel(); + + con = new GridBagConstraints(); + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 0; + con.gridy = 0; + con.weightx = 1; + layout.setConstraints(spacer1, con); + + con = new GridBagConstraints(); + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 1; + con.gridy = 0; + con.insets = new Insets(5, 5, 5, 5); + con.weightx = 0; + layout.setConstraints(blue, con); + + con = new GridBagConstraints(); + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 2; + con.gridy = 0; + con.insets = new Insets(5, 5, 5, 5); + con.weightx = 0; + layout.setConstraints(green, con); + + con = new GridBagConstraints(); + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 3; + con.gridy = 0; + con.insets = new Insets(5, 5, 5, 5); + con.weightx = 0; + layout.setConstraints(red, con); + + con = new GridBagConstraints(); + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 4; + con.gridy = 0; + con.insets = new Insets(5, 5, 5, 5); + con.weightx = 0; + layout.setConstraints(yellow, con); + + con = new GridBagConstraints(); + con.gridheight = 1; + con.gridwidth = 1; + con.gridx = 5; + con.gridy = 0; + con.weightx = 1; + layout.setConstraints(spacer2, con); + + setLayout(layout); + add(spacer1); + add(blue); + add(green); + add(red); + add(yellow); + add(spacer2); + } +}