diff --git a/black-bishop.png b/black-bishop.png new file mode 100644 index 0000000..c4dca86 Binary files /dev/null and b/black-bishop.png differ diff --git a/black-king.png b/black-king.png new file mode 100644 index 0000000..2e90c45 Binary files /dev/null and b/black-king.png differ diff --git a/black-knight.png b/black-knight.png new file mode 100644 index 0000000..2271f2a Binary files /dev/null and b/black-knight.png differ diff --git a/black-pawn.png b/black-pawn.png new file mode 100644 index 0000000..b59ed2d Binary files /dev/null and b/black-pawn.png differ diff --git a/black-queen.png b/black-queen.png new file mode 100644 index 0000000..fd78262 Binary files /dev/null and b/black-queen.png differ diff --git a/black-rook.png b/black-rook.png new file mode 100644 index 0000000..65b3585 Binary files /dev/null and b/black-rook.png differ diff --git a/src/Bishop.java b/src/Bishop.java index 75e96e7..6b47dfd 100644 --- a/src/Bishop.java +++ b/src/Bishop.java @@ -1,8 +1,8 @@ import java.util.ArrayList; public class Bishop extends Piece { - public Bishop(boolean black) { - super(black); + public Bishop(boolean black, DrawingPanel panel) { + super(black, panel, "black-bishop.png", "white-bishop.png"); } public ArrayList getPossibleMoves(BoardCoordinate position) { diff --git a/src/Board.java b/src/Board.java index 30f05c2..b48e736 100644 --- a/src/Board.java +++ b/src/Board.java @@ -1,5 +1,5 @@ -import java.awt.Color; -import java.awt.Graphics; +import java.awt.*; +import java.awt.image.ImageObserver; import java.util.ArrayList; public class Board { @@ -44,21 +44,21 @@ public class Board { if(i == 0 || i == 7){ for(int j = 0; j <= 7; j++){ if(j == 0 || j == 7){ - set(j, i, new Rook(i==0)); + set(j, i, new Rook(i==0, panel)); } else if(j == 1 || j == 6){ - set(j, i, new Knight(i==0)); + set(j, i, new Knight(i==0, panel)); } else if(j == 2 || j == 5){ - set(j, i, new Bishop(i==0)); + set(j, i, new Bishop(i==0, panel)); } else if(j == 4){ - set(j, i, new Queen(i==0)); + set(j, i, new King(i==0, panel)); } else { - set(j, i, new King(i==0)); + set(j, i, new Queen(i==0, panel)); } } } if(i == 1 || i == 6){ for(int j = 0; j <= 7; j++){ - set(j, i, new Pawn(i==1)); + set(j, i, new Pawn(i==1, panel)); } } } @@ -142,10 +142,13 @@ public class Board { } public void draw(ScreenCoordinate mousePosition) { + + + // Draw board - graphics.setColor(WHITE); - graphics.fillRect(0, 0, DIMENSION, DIMENSION); graphics.setColor(BLACK); + graphics.fillRect(0, 0, DIMENSION, DIMENSION); + graphics.setColor(WHITE); for (int y = 0; y < BOARD_SIZE; y++) for (int x = y % 2; x < BOARD_SIZE; x += 2) graphics.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE); @@ -154,7 +157,7 @@ public class Board { forEachPiece((boardCoordinate, piece) -> { // If piece is the one being dragged, render it at the mouse position // Otherwise, render it at the center of the board tile - piece.draw(graphics, boardCoordinate.equals(dragging) ? mousePosition : boardCoordinate.toScreen()); + piece.draw(graphics, panel, boardCoordinate.equals(dragging) ? mousePosition : boardCoordinate.toScreen()); }); } diff --git a/src/King.java b/src/King.java index 44419f8..7510129 100644 --- a/src/King.java +++ b/src/King.java @@ -1,8 +1,8 @@ import java.util.ArrayList; public class King extends Piece { - public King(boolean black) { - super(black); + public King(boolean black, DrawingPanel panel) { + super(black, panel, "black-king.png", "white-king.png"); } public ArrayList getPossibleMoves(BoardCoordinate position) { diff --git a/src/Knight.java b/src/Knight.java index 5c1489c..f7c86d7 100644 --- a/src/Knight.java +++ b/src/Knight.java @@ -1,8 +1,9 @@ +import java.awt.*; import java.util.ArrayList; public class Knight extends Piece { - public Knight(boolean black) { - super(black); + public Knight(boolean black, DrawingPanel panel) { + super(black, panel, "black-knight.png", "white-knight.png"); } public ArrayList getPossibleMoves(BoardCoordinate position) { diff --git a/src/Pawn.java b/src/Pawn.java index 2563766..bc73ad4 100644 --- a/src/Pawn.java +++ b/src/Pawn.java @@ -1,8 +1,8 @@ import java.util.ArrayList; public class Pawn extends Piece { - public Pawn(boolean black) { - super(black); + public Pawn(boolean black, DrawingPanel panel) { + super(black, panel, "black-pawn.png", "white-pawn.png"); } public ArrayList getPossibleMoves(BoardCoordinate position) { diff --git a/src/Piece.java b/src/Piece.java index d081207..fb23903 100644 --- a/src/Piece.java +++ b/src/Piece.java @@ -1,16 +1,16 @@ import java.awt.*; +import java.awt.image.ImageObserver; import java.util.ArrayList; public abstract class Piece { // Width and height of placeholder rectangle graphic - public static final int DIMENSION = 32; + static final int DIMENSION = 48; + Image image; public boolean black; - // If no parameter, default to white - public Piece() { } - - public Piece(boolean black) { + public Piece(boolean black, DrawingPanel panel, String blackImagePath, String whiteImagePath) { this.black = black; + image = panel.loadImage(black ? blackImagePath : whiteImagePath); } public abstract ArrayList getPossibleMoves(BoardCoordinate position); @@ -39,15 +39,11 @@ public abstract class Piece { // The Piece class doesn't store position, // so when drawing we need to be provided this along with a graphics context when drawing - public void draw(Graphics graphics, int x, int y) { - graphics.setColor(black ? Color.BLACK : Color.WHITE); - // Drawing is performed from the top-left corner. - // We need the drawn rectangle to be offset by half of the width and height - // so it is centered on the provided position. - graphics.fillRect(x - DIMENSION / 2, y - DIMENSION / 2, DIMENSION, DIMENSION); + public void draw(Graphics graphics, ImageObserver observer, int x, int y) { + graphics.drawImage(image, x - DIMENSION / 2, y - DIMENSION / 2, observer); } - public void draw(Graphics graphics, ScreenCoordinate coordinate) { - draw(graphics, coordinate.x, coordinate.y); + public void draw(Graphics graphics, ImageObserver observer, ScreenCoordinate coordinate) { + draw(graphics, observer, coordinate.x, coordinate.y); } } diff --git a/src/Queen.java b/src/Queen.java index 44f36db..c388211 100644 --- a/src/Queen.java +++ b/src/Queen.java @@ -1,8 +1,8 @@ import java.util.ArrayList; public class Queen extends Piece { - public Queen(boolean black) { - super(black); + public Queen(boolean black, DrawingPanel panel) { + super(black, panel, "black-queen.png", "white-queen.png"); } public ArrayList getPossibleMoves(BoardCoordinate position) { diff --git a/src/Rook.java b/src/Rook.java index d94ecf8..30dcd16 100644 --- a/src/Rook.java +++ b/src/Rook.java @@ -1,8 +1,8 @@ import java.util.ArrayList; public class Rook extends Piece { - public Rook(boolean black) { - super(black); + public Rook(boolean black, DrawingPanel panel) { + super(black, panel, "black-rook.png", "white-rook.png"); } public ArrayList getPossibleMoves(BoardCoordinate position) { diff --git a/white-bishop.png b/white-bishop.png new file mode 100644 index 0000000..287fb5e Binary files /dev/null and b/white-bishop.png differ diff --git a/white-king.png b/white-king.png new file mode 100644 index 0000000..a18943e Binary files /dev/null and b/white-king.png differ diff --git a/white-knight.png b/white-knight.png new file mode 100644 index 0000000..6e1a1e0 Binary files /dev/null and b/white-knight.png differ diff --git a/white-pawn.png b/white-pawn.png new file mode 100644 index 0000000..80f8d00 Binary files /dev/null and b/white-pawn.png differ diff --git a/white-queen.png b/white-queen.png new file mode 100644 index 0000000..5b62a60 Binary files /dev/null and b/white-queen.png differ diff --git a/white-rook.png b/white-rook.png new file mode 100644 index 0000000..1c5dc73 Binary files /dev/null and b/white-rook.png differ