Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added Javadoc for most of the GUI classes. The event listener is now …
…implemented.
  • Loading branch information
john committed Apr 14, 2016
1 parent 2fd01ce commit 5913738
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 67 deletions.
1 change: 1 addition & 0 deletions src/test/GUITest.java
Expand Up @@ -28,5 +28,6 @@ public class GUITest {
// handle exception
}
CheckersWindow window = new CheckersWindow();
window.open();
}
}
1 change: 1 addition & 0 deletions src/view/Checker.java
Expand Up @@ -8,6 +8,7 @@ import javax.swing.*;
* @author john
*
*/
@SuppressWarnings("serial")
public class Checker extends JPanel {

/* The color of the checker */
Expand Down
1 change: 1 addition & 0 deletions src/view/CheckersCanvas.java
Expand Up @@ -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;
Expand Down
75 changes: 41 additions & 34 deletions 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;


Expand All @@ -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, "<html><ol><li>instr 1</li></ol></html>", "title", JOptionPane.INFORMATION_MESSAGE);
//JOptionPane.showMessageDialog(null, "<html>instr 1<br></html>", "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);
Expand Down
25 changes: 19 additions & 6 deletions src/view/GameEventListener.java
Expand Up @@ -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
Expand All @@ -32,7 +37,7 @@ public class GameEventListener implements MouseListener, KeyListener, ActionList
}

}

@Override
public void mouseReleased(MouseEvent e) {}

Expand All @@ -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, "<html><ol><li>instr 1</li>"
+ "</ol></html>", "Instructions", JOptionPane.INFORMATION_MESSAGE);
}
}
}

17 changes: 12 additions & 5 deletions src/view/GamePanel.java
Expand Up @@ -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;
Expand All @@ -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++;
}
Expand Down
60 changes: 38 additions & 22 deletions src/view/Square.java
Expand Up @@ -11,6 +11,7 @@ import model.Location;
* @author john
*
*/
@SuppressWarnings("serial")
public class Square extends JPanel implements MouseListener {

/**
Expand Down Expand Up @@ -42,31 +43,53 @@ 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);
piece.addMouseListener(this);
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 <code>selected</code> member to.
*/
public void setSelected(boolean val) {
if (val) {
this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
Expand All @@ -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 <code>true</code> if the square contains a checker. <br />
* <code>false</code> 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) {}


}

0 comments on commit 5913738

Please sign in to comment.