From 40bb19a950b52097b314b7f0df87732aced289fa Mon Sep 17 00:00:00 2001 From: aah13002 Date: Wed, 30 Mar 2016 17:37:06 -0400 Subject: [PATCH] partially finished checkers canvas --- src/model/Game.java | 1 + src/test/GUITest.java | 10 +++++++ src/test/Square.java | 37 ++++++++++++++++++++++++ src/view/CheckersCanvas.java | 54 ++++++++++++++++++++++++++++++++++++ src/view/CheckersWindow.java | 30 ++++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 src/test/GUITest.java create mode 100644 src/test/Square.java create mode 100644 src/view/CheckersCanvas.java create mode 100644 src/view/CheckersWindow.java diff --git a/src/model/Game.java b/src/model/Game.java index f1d2ae8..6db6ad7 100755 --- a/src/model/Game.java +++ b/src/model/Game.java @@ -8,6 +8,7 @@ public Game(Player p1, Player p2) { this.player_one = p1; this.player_two = p2; this.board = new Board(); + assignPieces(); } private void assignPieces() { diff --git a/src/test/GUITest.java b/src/test/GUITest.java new file mode 100644 index 0000000..59f3532 --- /dev/null +++ b/src/test/GUITest.java @@ -0,0 +1,10 @@ +package test; + +import view.CheckersWindow; + +public class GUITest { + + public static void main(String[] args) { + CheckersWindow window = new CheckersWindow(); + } +} diff --git a/src/test/Square.java b/src/test/Square.java new file mode 100644 index 0000000..8ff6ee3 --- /dev/null +++ b/src/test/Square.java @@ -0,0 +1,37 @@ +package test; + +import java.awt.Color; + +import javax.swing.JButton; + +public class Square extends JButton { + private Color color; + private int row; + private int column; + + public Square(Color color, int row, int column) { + super(""); + this.color = color; + this.setRow(row); + this.setColumn(column); + this.setBackground(color); + } + + 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; + } + + +} diff --git a/src/view/CheckersCanvas.java b/src/view/CheckersCanvas.java new file mode 100644 index 0000000..43b7ab1 --- /dev/null +++ b/src/view/CheckersCanvas.java @@ -0,0 +1,54 @@ +package view; + +import java.awt.Color; +import java.awt.GridBagConstraints; +import java.awt.GridLayout; +import java.util.ArrayList; + +import javax.swing.JPanel; + +import model.Board; +import test.Square; + +public class CheckersCanvas extends JPanel { + + public static final int BOARD_DIM = 8; + + ArrayList blackSquares; + ArrayList redSquares; + + /* Checker board representation */ + Board board; + GridBagConstraints layoutConstraints; + + public CheckersCanvas() { + super(new GridLayout(BOARD_DIM, BOARD_DIM)); + this.blackSquares = new ArrayList(); + this.redSquares = new ArrayList(); + initSquares(); + } + + public void initSquares() { + for (int i = 0; i < BOARD_DIM; ++i) { + if (i % 2 == 0) { + for (int j = 0; j < BOARD_DIM/4; ++j) { + Square black = new Square(Color.BLACK, i, j*2); + Square red = new Square(Color.RED, i, j*2 + 1); + this.add(black); + blackSquares.add(black); + this.add(red); + redSquares.add(red); + } + } else { + for (int j = 0; j < BOARD_DIM/4; ++j) { + Square black = new Square(Color.BLACK, i, j*2 + 1); + Square red = new Square(Color.RED, i, j*2); + this.add(red); + blackSquares.add(red); + this.add(black); + redSquares.add(black); + } + } + } + } +} diff --git a/src/view/CheckersWindow.java b/src/view/CheckersWindow.java new file mode 100644 index 0000000..c7ad350 --- /dev/null +++ b/src/view/CheckersWindow.java @@ -0,0 +1,30 @@ +package view; + +import javax.swing.JFrame; + +public class CheckersWindow extends JFrame { + + public static final int HEIGHT = 800; + public static final int WIDTH = 800; + private CheckersCanvas canvas; + + public CheckersWindow() { + super("Checkers"); + this.setSize(WIDTH, HEIGHT); + this.setDefaultCloseOperation(EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + this.getContentPane().add(new CheckersCanvas()); + this.setVisible(true); + + //pack(); + } + + public CheckersCanvas getCanvas() { + return canvas; + } + + public void setCanvas(CheckersCanvas canvas) { + this.canvas = canvas; + } + +}