Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
updated ui
  • Loading branch information
john committed Apr 14, 2016
1 parent d1ddbdf commit 2fd01ce
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 130 deletions.
6 changes: 5 additions & 1 deletion src/model/Game.java → 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;
Expand Down
29 changes: 29 additions & 0 deletions 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;
}


}
38 changes: 0 additions & 38 deletions src/view/BoardEventListener.java

This file was deleted.

12 changes: 5 additions & 7 deletions src/view/Checker.java
Expand Up @@ -8,26 +8,24 @@ import javax.swing.*;
* @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);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions src/view/CheckersCanvas.java
Expand Up @@ -4,8 +4,6 @@ import java.awt.Color;
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;

Expand All @@ -21,50 +19,50 @@ 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];

this.setPreferredSize(new Dimension(CANVAS_SIZE, CANVAS_SIZE));
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 {
for (int j = 0; j < BOARD_DIM/2; ++j) {
/* 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);
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/view/CheckersWindow.java
Expand Up @@ -16,9 +16,10 @@ import javax.swing.JOptionPane;

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() {
Expand All @@ -27,14 +28,17 @@ public class CheckersWindow extends JFrame {
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");
Expand Down Expand Up @@ -80,8 +84,9 @@ public class CheckersWindow extends JFrame {


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

}
63 changes: 63 additions & 0 deletions 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);
}
}

0 comments on commit 2fd01ce

Please sign in to comment.