Skip to content

Commit

Permalink
Made changes to the view module to better separate things
Browse files Browse the repository at this point in the history
  • Loading branch information
john committed Apr 13, 2016
1 parent 54d4060 commit 0315026
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 115 deletions.
89 changes: 19 additions & 70 deletions src/view/CheckersCanvas.java
Original file line number Diff line number Diff line change
@@ -1,65 +1,70 @@
package view;

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 java.util.ArrayList;

import javax.swing.JPanel;

import model.Board;
import model.Location;
import model.Piece;

public class CheckersCanvas extends JPanel implements MouseListener {
public class CheckersCanvas extends JPanel {

public static final int BOARD_DIM = 8;
public static final int CANVAS_SIZE = 800;

private Square[][] board;

private Square moveDestination;
private Square moveSource;


GridBagConstraints layoutConstraints;

public CheckersCanvas() {
public CheckersCanvas(GamePanel gamePanel) {
super(new GridLayout(BOARD_DIM, BOARD_DIM));
this.board = new Square[BOARD_DIM][BOARD_DIM];
initSquares();

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

private void initCanvas(GamePanel gamePanel) {
initSquares(gamePanel);
initCheckers();
}

public void initSquares() {
private void initSquares(GamePanel gamePanel) {
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(this);
blackSquare.addMouseListener(gamePanel);
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(this);
redSquare.addMouseListener(gamePanel);
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(this);
redSquare.addMouseListener(gamePanel);
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(this);
blackSquare.addMouseListener(gamePanel);
this.add(blackSquare);
}
}
Expand All @@ -76,60 +81,4 @@ private void initCheckers() {
}
}
}

@Override
public void mouseClicked(MouseEvent arg0) {


}

@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) {
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;
} else {
if (square == moveSource)
moveSource = null;
square.setSelected(false);
}
} else {

if (!square.isSelected()) {
square.setSelected(true);
if (moveDestination != null)
moveDestination.setSelected(false);
moveDestination = square;
} else {
if (square == moveDestination)
moveDestination = null;
square.setSelected(false);
}
}

}

@Override
public void mouseReleased(MouseEvent e) {

}
}
23 changes: 10 additions & 13 deletions src/view/CheckersWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,28 @@
import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CheckersWindow extends JFrame {

public static final int HEIGHT = 800;
public static final int HEIGHT = 825;
public static final int WIDTH = 800;
private CheckersCanvas canvas;
private GamePanel gamePanel;

public CheckersWindow() {
super("Checkers");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.getContentPane().add(new CheckersCanvas(), BorderLayout.CENTER);
initGamePanel();
this.setVisible(true);
this.setResizable(false);

//pack();
}

public CheckersCanvas getCanvas() {
return canvas;
}

public void setCanvas(CheckersCanvas canvas) {
this.canvas = canvas;

private void initGamePanel() {
this.gamePanel = new GamePanel();
this.getContentPane().add(this.gamePanel, BorderLayout.CENTER);
}

}
91 changes: 91 additions & 0 deletions src/view/GamePanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package view;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;

public class GamePanel extends JPanel implements MouseListener {
private JLabel messageBar;
private CheckersCanvas canvas;

private Square moveDestination;
private Square moveSource;

public GamePanel() {
this.messageBar = new JLabel("Select a piece to move.");
this.canvas = new CheckersCanvas(this);

this.add(this.messageBar);
this.add(this.canvas);
}

public void displayMessage(String message) {
this.messageBar.setText(message);
}

@Override
public void mouseClicked(MouseEvent arg0) {


}

@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) {
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.");
}
} 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.");
}
}

}

@Override
public void mouseReleased(MouseEvent e) {

}
}
32 changes: 0 additions & 32 deletions src/view/RoundedBorder.java

This file was deleted.

0 comments on commit 0315026

Please sign in to comment.