diff --git a/src/test/GUITest.java b/src/test/GUITest.java index 9a58bbc..0cacc2d 100644 --- a/src/test/GUITest.java +++ b/src/test/GUITest.java @@ -28,5 +28,6 @@ public class GUITest { // handle exception } CheckersWindow window = new CheckersWindow(); + window.open(); } } diff --git a/src/view/Checker.java b/src/view/Checker.java index 7ab90f6..421aeba 100644 --- a/src/view/Checker.java +++ b/src/view/Checker.java @@ -8,6 +8,7 @@ import javax.swing.*; * @author john * */ +@SuppressWarnings("serial") public class Checker extends JPanel { /* The color of the checker */ diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 5a62611..ee2b11a 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -9,6 +9,7 @@ import javax.swing.JPanel; import model.Location; +@SuppressWarnings("serial") public class CheckersCanvas extends JPanel { public static final int BOARD_DIM = 8; diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java index 6036ae7..a1e92bc 100644 --- a/src/view/CheckersWindow.java +++ b/src/view/CheckersWindow.java @@ -1,24 +1,28 @@ package view; -import java.awt.BorderLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; -import javax.swing.JOptionPane; - +@SuppressWarnings("serial") public class CheckersWindow extends JFrame { - + /* Constants for the size of the window */ public static final int HEIGHT = 868; public static final int WIDTH = 800; + + /** + * The {@link GamePanel} instance which contains all of the game's visual + * components. + */ private GamePanel gamePanel; + + /** + * The {@link GameEventListener} instance which will be listening to all + * of the game panel's component's + */ private GameEventListener gameListener; @@ -28,61 +32,64 @@ public class CheckersWindow extends JFrame { this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setResizable(false); this.setLocationRelativeTo(null); - this.createMenuBar(); + + /* MUST be done in this order */ this.initGameListener(); + this.createMenuBar(); this.initGamePanel(); - this.setVisible(true); this.pack(); } + /** + * Opens the window by setting the frame's visibility + */ + public void open() { + this.setVisible(true); + } + + /** + * Initializes the {@link GameEventListener} instance. + */ private void initGameListener() { this.gameListener = new GameEventListener(); } + /** + * Initializes the frame's menu bar and all of its items. + */ private void createMenuBar() { JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); - //New Game + /* New Game Option */ JMenuItem newGame = new JMenuItem("New game"); newGame.setMnemonic(KeyEvent.VK_N); newGame.setToolTipText("Start a new game"); - newGame.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - //this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER); - } - }); - //Quit + newGame.addActionListener(gameListener); + + /* Quit Option */ JMenuItem quit = new JMenuItem("Quit"); quit.setMnemonic(KeyEvent.VK_Q); quit.setToolTipText("Exit application"); - quit.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - System.exit(0); - } - }); - //Rules + quit.addActionListener(gameListener); + + /* Instructions Option */ JMenuItem instructions = new JMenuItem("Instructions"); instructions.setMnemonic(KeyEvent.VK_I); instructions.setToolTipText("How to play"); - instructions.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(null, "
  1. instr 1
", "title", JOptionPane.INFORMATION_MESSAGE); - //JOptionPane.showMessageDialog(null, "instr 1
", "title", JOptionPane.INFORMATION_MESSAGE); - } - }); + instructions.addActionListener(gameListener); + /* Add items to menu bar */ file.add(quit); file.add(newGame); file.add(instructions); menubar.add(file); menubar.setVisible(true); - setJMenuBar(menubar); + this.setJMenuBar(menubar); } - + /** + * Initializes the {@link GamePanel} instance + */ private void initGamePanel() { this.gamePanel = new GamePanel(gameListener); this.getContentPane().add(this.gamePanel); diff --git a/src/view/GameEventListener.java b/src/view/GameEventListener.java index 078d0a9..b57aa30 100644 --- a/src/view/GameEventListener.java +++ b/src/view/GameEventListener.java @@ -7,8 +7,13 @@ import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; +import javax.swing.JOptionPane; + public class GameEventListener implements MouseListener, KeyListener, ActionListener { - + + /** + * An instance of {@link GamePanel} for which this listener is listening. + */ private GamePanel gamePanel; @Override @@ -32,7 +37,7 @@ public class GameEventListener implements MouseListener, KeyListener, ActionList } } - + @Override public void mouseReleased(MouseEvent e) {} @@ -45,19 +50,27 @@ public class GameEventListener implements MouseListener, KeyListener, ActionList 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); - } + if (e.getActionCommand().equals("New game")) { + // TODO: Request the controller for a new game + } else if (e.getActionCommand().equals("Quit")) { + System.exit(0); + } else if (e.getActionCommand().equals("Instructions")) { + // TODO: Create an instructions dialog box, this is just for testing + JOptionPane.showMessageDialog(null, "
  1. instr 1
  2. " + + "
", "Instructions", JOptionPane.INFORMATION_MESSAGE); + } + } } diff --git a/src/view/GamePanel.java b/src/view/GamePanel.java index 8a6fc2d..a78054f 100644 --- a/src/view/GamePanel.java +++ b/src/view/GamePanel.java @@ -6,6 +6,7 @@ import java.awt.GridBagLayout; import javax.swing.JLabel; import javax.swing.JPanel; +@SuppressWarnings("serial") public class GamePanel extends JPanel { private JLabel messageBar; private CheckersCanvas canvas; @@ -16,23 +17,29 @@ public class GamePanel extends JPanel { public GamePanel(GameEventListener gameListener) { super(new GridBagLayout()); + + /* Initialize the layout manager */ this.layoutConstraints = new GridBagConstraints(); - this.messageBar = new JLabel("Select a piece to move."); + this.layoutConstraints.gridy = 0; + + /* Add the game listener as a listener */ this.addKeyListener(gameListener); - this.canvas = new CheckersCanvas(gameListener); this.setFocusable(true); - this.layoutConstraints.gridy = 0; + + /* Initialize the components */ this.initMessageBar(); - this.initCanvas(); + this.initCanvas(gameListener); } private void initMessageBar() { + this.messageBar = new JLabel("Select a piece to move."); this.layoutConstraints.ipady = 10; this.add(this.messageBar, layoutConstraints); this.layoutConstraints.gridy++; } - private void initCanvas() { + private void initCanvas(GameEventListener gameListener) { + this.canvas = new CheckersCanvas(gameListener); this.add(this.canvas, layoutConstraints); this.layoutConstraints.gridy++; } diff --git a/src/view/Square.java b/src/view/Square.java index da9faec..75d7bfc 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -11,6 +11,7 @@ import model.Location; * @author john * */ +@SuppressWarnings("serial") public class Square extends JPanel implements MouseListener { /** @@ -42,20 +43,34 @@ public class Square extends JPanel implements MouseListener { } + /** + * Initializes the properties of the square such as color and layout. + * @param color A {@link Color} object representing the square's color. + */ private void initSquare(Color color) { this.setBackground(color); this.setLayout(new BorderLayout()); } + /** + * {@link Square#location} + */ public Location getCellLocation() { return location; } + /** + * {@link Square#piece} + */ public Checker getPiece() { return piece; } + /** + * Adds the given piece to the square's panel. + * @param piece A {@link Checker} object to place in the square. + */ public void setPiece(Checker piece) { this.piece = piece; this.add(piece); @@ -63,10 +78,18 @@ public class Square extends JPanel implements MouseListener { this.validate(); } + /** + * {@link Square#selected} + */ public boolean isSelected() { return this.selected; } + /** + * Set's the {@link Square#selected} member to the given value and updates + * the panel's border. + * @param val The boolean value to set the selected member to. + */ public void setSelected(boolean val) { if (val) { this.setBorder(BorderFactory.createLineBorder(Color.WHITE)); @@ -76,41 +99,34 @@ public class Square extends JPanel implements MouseListener { this.selected = val; } + /** + * Check's if the square contains a {@link Checker} object or not. + * @return true if the square contains a checker.
+ * false otherwise. + */ public boolean hasPiece() { return this.piece != null; } - + @Override - public void mouseClicked(MouseEvent arg0) { - // TODO Auto-generated method stub - + public void mousePressed(MouseEvent e) { + /* Send the event to the lister of the square (GameEventListener) */ + MouseEvent newE = new MouseEvent(this, e.getID(), e.getWhen(), e.getModifiers(), + e.getClickCount(), e.getX(), e.getY(), e.isPopupTrigger()); + this.dispatchEvent(newE); } @Override - public void mouseEntered(MouseEvent arg0) { - // TODO Auto-generated method stub - - } + public void mouseClicked(MouseEvent arg0) {} @Override - public void mouseExited(MouseEvent arg0) { - // TODO Auto-generated method stub - - } + public void mouseEntered(MouseEvent arg0) {} @Override - 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); - - } + public void mouseExited(MouseEvent arg0) {} @Override - public void mouseReleased(MouseEvent arg0) { - // TODO Auto-generated method stub - - } + public void mouseReleased(MouseEvent arg0) {} }