From 54d4060c52d170dba89b3b7ab3bbea61f404f5da Mon Sep 17 00:00:00 2001 From: jwb11006 Date: Tue, 12 Apr 2016 04:46:24 -0400 Subject: [PATCH] finished up GUI for the checkers board. Pieces are now layed out properly with the correct colors. --- src/model/Location.java | 16 ++++++ src/view/{GUIPiece.java => Checker.java} | 12 ++--- src/view/CheckersCanvas.java | 64 +++++++++++++++--------- src/view/Square.java | 36 ++++--------- 4 files changed, 69 insertions(+), 59 deletions(-) create mode 100644 src/model/Location.java rename src/view/{GUIPiece.java => Checker.java} (79%) diff --git a/src/model/Location.java b/src/model/Location.java new file mode 100644 index 0000000..6ea1574 --- /dev/null +++ b/src/model/Location.java @@ -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; + } +} diff --git a/src/view/GUIPiece.java b/src/view/Checker.java similarity index 79% rename from src/view/GUIPiece.java rename to src/view/Checker.java index 159b281..b4fed20 100644 --- a/src/view/GUIPiece.java +++ b/src/view/Checker.java @@ -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); @@ -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); } diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java index 1822137..498d31a 100644 --- a/src/view/CheckersCanvas.java +++ b/src/view/CheckersCanvas.java @@ -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 blackSquares; - private ArrayList 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(); - this.redSquares = new ArrayList(); + 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) { diff --git a/src/view/Square.java b/src/view/Square.java index 8f0a264..891c8bc 100644 --- a/src/view/Square.java +++ b/src/view/Square.java @@ -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); @@ -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);