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; import java.util.ArrayList;
public class Bishop extends Piece { public class Bishop extends Piece {
public Bishop(boolean black) { public Bishop(boolean black, DrawingPanel panel) {
super(black); super(black, panel, "black-bishop.png", "white-bishop.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -1,5 +1,5 @@
import java.awt.Color; import java.awt.*;
import java.awt.Graphics; import java.awt.image.ImageObserver;
import java.util.ArrayList; import java.util.ArrayList;
public class Board { public class Board {
@ -44,21 +44,21 @@ public class Board {
if(i == 0 || i == 7){ if(i == 0 || i == 7){
for(int j = 0; j <= 7; j++){ for(int j = 0; j <= 7; j++){
if(j == 0 || j == 7){ 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){ } 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){ } 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){ } else if(j == 4){
set(j, i, new Queen(i==0)); set(j, i, new King(i==0, panel));
} else { } else {
set(j, i, new King(i==0)); set(j, i, new Queen(i==0, panel));
} }
} }
} }
if(i == 1 || i == 6){ if(i == 1 || i == 6){
for(int j = 0; j <= 7; j++){ 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) { public void draw(ScreenCoordinate mousePosition) {
// Draw board // Draw board
graphics.setColor(WHITE);
graphics.fillRect(0, 0, DIMENSION, DIMENSION);
graphics.setColor(BLACK); graphics.setColor(BLACK);
graphics.fillRect(0, 0, DIMENSION, DIMENSION);
graphics.setColor(WHITE);
for (int y = 0; y < BOARD_SIZE; y++) for (int y = 0; y < BOARD_SIZE; y++)
for (int x = y % 2; x < BOARD_SIZE; x += 2) for (int x = y % 2; x < BOARD_SIZE; x += 2)
graphics.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE); graphics.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
@ -154,7 +157,7 @@ public class Board {
forEachPiece((boardCoordinate, piece) -> { forEachPiece((boardCoordinate, piece) -> {
// If piece is the one being dragged, render it at the mouse position // If piece is the one being dragged, render it at the mouse position
// Otherwise, render it at the center of the board tile // 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; import java.util.ArrayList;
public class King extends Piece { public class King extends Piece {
public King(boolean black) { public King(boolean black, DrawingPanel panel) {
super(black); super(black, panel, "black-king.png", "white-king.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -1,8 +1,9 @@
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
public class Knight extends Piece { public class Knight extends Piece {
public Knight(boolean black) { public Knight(boolean black, DrawingPanel panel) {
super(black); super(black, panel, "black-knight.png", "white-knight.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -1,8 +1,8 @@
import java.util.ArrayList; import java.util.ArrayList;
public class Pawn extends Piece { public class Pawn extends Piece {
public Pawn(boolean black) { public Pawn(boolean black, DrawingPanel panel) {
super(black); super(black, panel, "black-pawn.png", "white-pawn.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -1,16 +1,16 @@
import java.awt.*; import java.awt.*;
import java.awt.image.ImageObserver;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class Piece { public abstract class Piece {
// Width and height of placeholder rectangle graphic // Width and height of placeholder rectangle graphic
public static final int DIMENSION = 32; static final int DIMENSION = 48;
Image image;
public boolean black; public boolean black;
// If no parameter, default to white public Piece(boolean black, DrawingPanel panel, String blackImagePath, String whiteImagePath) {
public Piece() { }
public Piece(boolean black) {
this.black = black; this.black = black;
image = panel.loadImage(black ? blackImagePath : whiteImagePath);
} }
public abstract ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position); public abstract ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position);
@ -39,15 +39,11 @@ public abstract class Piece {
// The Piece class doesn't store position, // The Piece class doesn't store position,
// so when drawing we need to be provided this along with a graphics context when drawing // 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) { public void draw(Graphics graphics, ImageObserver observer, int x, int y) {
graphics.setColor(black ? Color.BLACK : Color.WHITE); graphics.drawImage(image, x - DIMENSION / 2, y - DIMENSION / 2, observer);
// 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, ScreenCoordinate coordinate) { public void draw(Graphics graphics, ImageObserver observer, ScreenCoordinate coordinate) {
draw(graphics, coordinate.x, coordinate.y); draw(graphics, observer, coordinate.x, coordinate.y);
} }
} }

@ -1,8 +1,8 @@
import java.util.ArrayList; import java.util.ArrayList;
public class Queen extends Piece { public class Queen extends Piece {
public Queen(boolean black) { public Queen(boolean black, DrawingPanel panel) {
super(black); super(black, panel, "black-queen.png", "white-queen.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) {

@ -1,8 +1,8 @@
import java.util.ArrayList; import java.util.ArrayList;
public class Rook extends Piece { public class Rook extends Piece {
public Rook(boolean black) { public Rook(boolean black, DrawingPanel panel) {
super(black); super(black, panel, "black-rook.png", "white-rook.png");
} }
public ArrayList<BoardCoordinate> getPossibleMoves(BoardCoordinate position) { 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