Skip to content

Commit

Permalink
finished up GUI for the checkers board. Pieces are now layed out prop…
Browse files Browse the repository at this point in the history
…erly with the correct colors.
  • Loading branch information
john committed Apr 12, 2016
1 parent ad79f6e commit 54d4060
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 59 deletions.
16 changes: 16 additions & 0 deletions src/model/Location.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package model;

/**
* Represents a (row, column) location on a chess board.
* @author john
*
*/
public class Location {
public final int row;
public final int column;

public Location(int row, int column) {
this.row = row;
this.column = column;
}
}
12 changes: 4 additions & 8 deletions src/view/GUIPiece.java → src/view/Checker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import javax.swing.BorderFactory;
import javax.swing.JButton;

public class GUIPiece extends JButton {
public class Checker extends JButton {
private Color color;

public GUIPiece(Color color) {
public Checker(Color color) {
super("");
this.color = color;
this.setContentAreaFilled(false);
Expand All @@ -21,17 +21,13 @@ public GUIPiece(Color color) {
}

protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
g.setColor(color);
} else {
g.setColor(getBackground());
}
Graphics2D g2 = (Graphics2D)g;
Graphics2D g2 = (Graphics2D) g;
RenderingHints hints = new RenderingHints(null);
hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
hints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setRenderingHints(hints);
g2.setColor(color);
g2.fillOval(5, 5, getSize().width-10,getSize().height-10);
super.paintComponent(g2);
}
Expand Down
64 changes: 39 additions & 25 deletions src/view/CheckersCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,75 @@
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JPanel;

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

public class CheckersCanvas extends JPanel implements MouseListener {

public static final int BOARD_DIM = 8;

private ArrayList<Square> blackSquares;
private ArrayList<Square> redSquares;
private Square[][] board;

private Square moveDestination;
private Square moveSource;

/* Checker board representation */
Board board;

GridBagConstraints layoutConstraints;

public CheckersCanvas() {
super(new GridLayout(BOARD_DIM, BOARD_DIM));
this.blackSquares = new ArrayList<Square>();
this.redSquares = new ArrayList<Square>();
this.board = new Square[BOARD_DIM][BOARD_DIM];
initSquares();
initCheckers();
}

public void initSquares() {
for (int i = 0; i < BOARD_DIM; ++i) {
if (i % 2 == 0) {
for (int j = 0; j < BOARD_DIM/2; ++j) {
Square black = new Square(Color.BLACK, i, j*2);
black.addMouseListener(this);
GUIPiece p = new GUIPiece(Color.WHITE);
black.setPiece(new GUIPiece(Color.WHITE));
Square red = new Square(Color.RED, i, j*2 + 1);
red.addMouseListener(this);
this.add(black);
blackSquares.add(black);
this.add(red);
redSquares.add(red);
/* Create a black square */
Square blackSquare = new Square(Color.BLACK, new Location(i, j*2));
board[i][j*2] = blackSquare;
blackSquare.addMouseListener(this);
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);
this.add(redSquare);
}
} else {
for (int j = 0; j < BOARD_DIM/2; ++j) {
Square black = new Square(Color.BLACK, i, j*2 + 1);
black.addMouseListener(this);
Square red = new Square(Color.RED, i, j*2);
red.addMouseListener(this);
this.add(red);
blackSquares.add(red);
this.add(black);
redSquares.add(black);
/* 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);
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);
this.add(blackSquare);
}
}
}
}

private void initCheckers() {
for (int row = 0; row < BOARD_DIM / 2 - 1; ++row) {
for (int col = 0; col < BOARD_DIM / 2; ++col) {
Checker redChecker = new Checker(new Color(255, 51, 51));
Checker blackChecker = new Checker(new Color(89, 89, 89));
board[row][2*col+ (row % 2)].setPiece(redChecker);
board[BOARD_DIM - 1 - row][2*col + (BOARD_DIM - 1 - row) %2].setPiece(blackChecker);
}
}
}

@Override
public void mouseClicked(MouseEvent arg0) {
Expand Down
36 changes: 10 additions & 26 deletions src/view/Square.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,33 @@
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.*;
import model.Location;

public class Square extends JPanel implements MouseListener {
private Color color;
private int row;
private int column;
private GUIPiece piece;
private final Location location;
private Checker piece;
private boolean selected;

public Square(Color color, int row, int column) {
public Square(Color color, Location location) {
super();
this.color = color;
this.setRow(row);
this.location = location;
this.selected = false;
this.setColumn(column);
this.setBackground(color);
this.setLayout(new BorderLayout());
}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public int getColumn() {
return column;
}

public void setColumn(int column) {
this.column = column;
public Location getCellLocation() {
return location;
}

public GUIPiece getPiece() {
public Checker getPiece() {
return piece;
}

public void setPiece(GUIPiece piece) {
public void setPiece(Checker piece) {
this.piece = piece;
this.add(piece);
piece.addMouseListener(this);
Expand All @@ -58,7 +43,6 @@ public boolean isSelected() {

public void setSelected(boolean val) {
if (val) {
System.out.println("setting border");
this.setBorder(BorderFactory.createLineBorder(Color.WHITE));
} else {
this.setBorder(null);
Expand Down

0 comments on commit 54d4060

Please sign in to comment.