From 2fd01cea06d4221fa9c4058a01ba17bd223fa0d6 Mon Sep 17 00:00:00 2001 From: jwb11006 Date: Wed, 13 Apr 2016 20:24:05 -0400 Subject: [PATCH] updated ui --- src/{model => controller}/Game.java | 6 +- src/model/Move.java | 29 +++++++ src/view/BoardEventListener.java | 38 --------- src/view/Checker.java | 12 ++- src/view/CheckersCanvas.java | 20 +++-- src/view/CheckersWindow.java | 19 +++-- src/view/GameEventListener.java | 63 +++++++++++++++ src/view/GamePanel.java | 119 +++++++++++++--------------- src/view/Square.java | 6 +- 9 files changed, 182 insertions(+), 130 deletions(-) rename src/{model => controller}/Game.java (82%) create mode 100644 src/model/Move.java delete mode 100644 src/view/BoardEventListener.java create mode 100644 src/view/GameEventListener.java diff --git a/src/model/Game.java b/src/controller/Game.java similarity index 82% rename from src/model/Game.java rename to src/controller/Game.java index 6db6ad7..9f70302 100755 --- a/src/model/Game.java +++ b/src/controller/Game.java @@ -1,4 +1,8 @@ -package model; +package controller; + +import model.Board; +import model.Color; +import model.Player; public class Game { private Board board; diff --git a/src/model/Move.java b/src/model/Move.java new file mode 100644 index 0000000..033a321 --- /dev/null +++ b/src/model/Move.java @@ -0,0 +1,29 @@ +package model; + +public class Move { + private Location source; + private Location destination; + + public Move(Location source, Location destination) { + this.setSource(source); + this.setDestination(destination); + } + + public Location getSource() { + return source; + } + + public void setSource(Location source) { + this.source = source; + } + + public Location getDestination() { + return destination; + } + + public void setDestination(Location destination) { + this.destination = destination; + } + + +} diff --git a/src/view/BoardEventListener.java b/src/view/BoardEventListener.java deleted file mode 100644 index 5b8521c..0000000 --- a/src/view/BoardEventListener.java +++ /dev/null @@ -1,38 +0,0 @@ -package view; - -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; - -public class BoardEventListener implements MouseListener { - - @Override - public void mouseClicked(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseExited(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mousePressed(MouseEvent e) { - // TODO Auto-generated method stub - - } - - @Override - public void mouseReleased(MouseEvent e) { - // TODO Auto-generated method stub - - } - -} diff --git a/src/view/Checker.java b/src/view/Checker.java index 8754a1e..7ab90f6 100644 --- a/src/view/Checker.java +++ b/src/view/Checker.java @@ -8,26 +8,24 @@ * @author john * */ -public class Checker extends JButton { +public class Checker extends JPanel { /* The color of the checker */ private Color color; public Checker(Color color) { - super(""); + super(); this.color = color; - initButtonProperties(); + initChecker(); } /** * Initializes the properties of the Checker as a JButton */ - private void initButtonProperties() { - /* We want the JButton to actually be completely transparent */ - this.setContentAreaFilled(false); - this.setBorderPainted(false); + private void initChecker() { this.setFocusable(false); this.setOpaque(false); + this.setEnabled(false); } /** diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 33997a8..5a62611 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -4,8 +4,6 @@ import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridLayout; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; import javax.swing.JPanel; @@ -21,7 +19,7 @@ public class CheckersCanvas extends JPanel { GridBagConstraints layoutConstraints; - public CheckersCanvas(GamePanel gamePanel) { + public CheckersCanvas(GameEventListener boardListener) { super(new GridLayout(BOARD_DIM, BOARD_DIM)); this.board = new Square[BOARD_DIM][BOARD_DIM]; @@ -29,28 +27,28 @@ public CheckersCanvas(GamePanel gamePanel) { this.setMaximumSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE)); this.setMinimumSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE)); - initCanvas(gamePanel); + this.initCanvas(boardListener); } - private void initCanvas(GamePanel gamePanel) { - initSquares(gamePanel); + private void initCanvas(GameEventListener boardListener) { + initSquares(boardListener); initCheckers(); } - private void initSquares(GamePanel gamePanel) { + private void initSquares(GameEventListener boardListener) { for (int i = 0; i < BOARD_DIM; ++i) { if (i % 2 == 0) { for (int j = 0; j < BOARD_DIM/2; ++j) { /* Create a black square */ Square blackSquare = new Square(Color.BLACK, new Location(i, j*2)); board[i][j*2] = blackSquare; - blackSquare.addMouseListener(gamePanel); + blackSquare.addMouseListener(boardListener); this.add(blackSquare); /* Create a red square */ Square redSquare = new Square(new Color(150, 0, 0), new Location(i, j*2 + 1)); board[i][j*2 + 1] = redSquare; - redSquare.addMouseListener(gamePanel); + redSquare.addMouseListener(boardListener); this.add(redSquare); } } else { @@ -58,13 +56,13 @@ private void initSquares(GamePanel gamePanel) { /* Create a red square */ Square redSquare = new Square(new Color(150, 0, 0), new Location(i, j*2)); board[i][j*2] = redSquare; - redSquare.addMouseListener(gamePanel); + redSquare.addMouseListener(boardListener); this.add(redSquare); /* Create a black square */ Square blackSquare = new Square(Color.BLACK, new Location(i, j*2 + 1)); board[i][j*2 + 1] = blackSquare; - blackSquare.addMouseListener(gamePanel); + blackSquare.addMouseListener(boardListener); this.add(blackSquare); } } diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index e3cb180..6036ae7 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -16,9 +16,10 @@ public class CheckersWindow extends JFrame { - public static final int HEIGHT = 825; + public static final int HEIGHT = 868; public static final int WIDTH = 800; private GamePanel gamePanel; + private GameEventListener gameListener; public CheckersWindow() { @@ -27,14 +28,17 @@ public CheckersWindow() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); - initGamePanel(); this.createMenuBar(); + this.initGameListener(); + this.initGamePanel(); this.setVisible(true); - this.setResizable(false); - - //pack(); + this.pack(); } + private void initGameListener() { + this.gameListener = new GameEventListener(); + } + private void createMenuBar() { JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); @@ -80,8 +84,9 @@ public void actionPerformed(ActionEvent e) { private void initGamePanel() { - this.gamePanel = new GamePanel(); - this.getContentPane().add(this.gamePanel, BorderLayout.CENTER); + this.gamePanel = new GamePanel(gameListener); + this.getContentPane().add(this.gamePanel); + this.gameListener.setGamePanel(gamePanel); } } diff --git a/src/view/GameEventListener.java b/src/view/GameEventListener.java new file mode 100644 index 0000000..078d0a9 --- /dev/null +++ b/src/view/GameEventListener.java @@ -0,0 +1,63 @@ +package view; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +public class GameEventListener implements MouseListener, KeyListener, ActionListener { + + private GamePanel gamePanel; + + @Override + public void mouseClicked(MouseEvent arg0) {} + + @Override + public void mouseEntered(MouseEvent e) {} + + @Override + public void mouseExited(MouseEvent e) {} + + @Override + public void mousePressed(MouseEvent e) { + Square square = (Square) e.getComponent(); + if(square.hasPiece()) { + gamePanel.setMoveSource(square); + gamePanel.updateMoveMessage(); + } else { + gamePanel.setMoveDestination(square); + gamePanel.updateMoveMessage(); + } + + } + + @Override + public void mouseReleased(MouseEvent e) {} + + @Override + public void keyPressed(KeyEvent arg0) {} + + @Override + public void keyReleased(KeyEvent e) { + if(e.getKeyCode() == KeyEvent.VK_ENTER && gamePanel.moveReady()) { + System.out.println("Moving piece."); + // TODO: Request a move from the controller. + } + + } + + @Override + public void keyTyped(KeyEvent arg0) {} + + public void setGamePanel(GamePanel gamePanel) { + this.gamePanel = gamePanel; + } + + @Override + public void actionPerformed(ActionEvent e) { + System.exit(0); + } +} + diff --git a/src/view/GamePanel.java b/src/view/GamePanel.java index 63264a9..8a6fc2d 100644 --- a/src/view/GamePanel.java +++ b/src/view/GamePanel.java @@ -1,91 +1,84 @@ package view; -import java.awt.event.MouseEvent; -import java.awt.event.MouseListener; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; import javax.swing.JLabel; import javax.swing.JPanel; -public class GamePanel extends JPanel implements MouseListener { +public class GamePanel extends JPanel { private JLabel messageBar; private CheckersCanvas canvas; - + private GridBagConstraints layoutConstraints; + private Square moveDestination; private Square moveSource; - - public GamePanel() { + + public GamePanel(GameEventListener gameListener) { + super(new GridBagLayout()); + this.layoutConstraints = new GridBagConstraints(); this.messageBar = new JLabel("Select a piece to move."); - this.canvas = new CheckersCanvas(this); - - this.add(this.messageBar); - this.add(this.canvas); + this.addKeyListener(gameListener); + this.canvas = new CheckersCanvas(gameListener); + this.setFocusable(true); + this.layoutConstraints.gridy = 0; + this.initMessageBar(); + this.initCanvas(); } - - public void displayMessage(String message) { - this.messageBar.setText(message); + + private void initMessageBar() { + this.layoutConstraints.ipady = 10; + this.add(this.messageBar, layoutConstraints); + this.layoutConstraints.gridy++; } - @Override - public void mouseClicked(MouseEvent arg0) { - - + private void initCanvas() { + this.add(this.canvas, layoutConstraints); + this.layoutConstraints.gridy++; } - @Override - public void mouseEntered(MouseEvent e) { - // TODO Auto-generated method stub + public void displayMessage(String message) { + this.messageBar.setText(message); + } + public void updateMoveMessage() { + if (moveSource == null) { + displayMessage("Select a piece to move."); + } else if (moveDestination == null) { + displayMessage("Select a destination."); + } else { + displayMessage("Select \"Move\" to move the piece."); + } } - @Override - public void mouseExited(MouseEvent e) { - // TODO Auto-generated method stub + public void setMoveSource(Square square) { + if (moveSource != null) moveSource.setSelected(false); + if (moveDestination != null) moveDestination.setSelected(false); + this.moveDestination = null; + + if (square == moveSource) { + this.moveSource = null; + } else { + this.moveSource = square; + square.setSelected(true); + } + } - @Override - public void mousePressed(MouseEvent e) { - Square square = (Square) e.getComponent(); - if(square.hasPiece()) { - if (!square.isSelected()) { - square.setSelected(true); - if (moveSource != null) - moveSource.setSelected(false); - if (moveDestination != null) - moveDestination.setSelected(false); - moveDestination = null; - moveSource = square; - displayMessage("Select a destination."); - } else { - if (square == moveSource) - moveSource = null; - square.setSelected(false); - displayMessage("Select a piece to move."); - } + public void setMoveDestination(Square square) { + if (moveDestination != null) moveDestination.setSelected(false); + + if (square == moveDestination) { + this.moveDestination = null; } else { - - if (!square.isSelected()) { - square.setSelected(true); - if (moveDestination != null) - moveDestination.setSelected(false); - moveDestination = square; - if (moveSource != null) - displayMessage("Select \"Move\" to move the piece."); - else - displayMessage("Select a piece to move."); - } else { - if (square == moveDestination) - moveDestination = null; - square.setSelected(false); - if (moveSource != null) - displayMessage("Select a destination."); - } + this.moveDestination = square; + square.setSelected(true); } - } - @Override - public void mouseReleased(MouseEvent e) { - + public boolean moveReady() { + return moveSource != null && moveDestination != null; } + } diff --git a/src/view/Square.java b/src/view/Square.java index ffc39cf..da9faec 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -64,7 +64,7 @@ public void setPiece(Checker piece) { } public boolean isSelected() { - return selected; + return this.selected; } public void setSelected(boolean val) { @@ -77,7 +77,7 @@ public void setSelected(boolean val) { } public boolean hasPiece() { - return piece != null; + return this.piece != null; } @Override @@ -102,7 +102,7 @@ public void mouseExited(MouseEvent arg0) { public void mousePressed(MouseEvent e) { MouseEvent newE = new MouseEvent(this, e.getID(), e.getWhen(), e.getModifiers(), e.getClickCount(), e.getX(), e.getY(), e.isPopupTrigger()); - this.dispatchEvent(newE); + //this.dispatchEvent(newE); }