Add piece graphics, flip white and black tiles, swap king and queen

main
Elnu 2 years ago
parent 17e81feac0
commit 08292bc9dd

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

@ -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<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

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

@ -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<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -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<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -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<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

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

@ -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<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -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<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB