Added pawn rendering

+ Initializing pawns in Board
+ Piece rendering in BoardPanel
- Fixed size initialization bug in BoardPanel
+ Pawn class
+ Color property in Piece
This commit is contained in:
Kai S. K. Engelbart 2019-07-01 19:37:15 +02:00
parent 0e99db6a46
commit b12c43fc8f
5 changed files with 63 additions and 7 deletions

View File

@ -1,5 +1,7 @@
package dev.kske.chess; package dev.kske.chess;
import dev.kske.chess.Piece.Color;
/** /**
* Project: <strong>Chess</strong><br> * Project: <strong>Chess</strong><br>
* File: <strong>Board.java</strong><br> * File: <strong>Board.java</strong><br>
@ -11,11 +13,16 @@ public class Board {
private Piece[][] boardArr; private Piece[][] boardArr;
public Board() { public Board() {
boardArr = new Piece[8][8];
initializeDefaultPositions(); initializeDefaultPositions();
} }
private void initializeDefaultPositions() { private void initializeDefaultPositions() {
// Initialize pawns
for (int i = 0; i < 8; i++) {
boardArr[i][1] = new Pawn(Color.BLACK);
boardArr[i][6] = new Pawn(Color.WHITE);
}
} }
/** /**

View File

@ -35,6 +35,7 @@ public class BoardPanel extends JPanel {
public BoardPanel(Board board) { public BoardPanel(Board board) {
this(); this();
setSize(getPreferredSize());
setBoard(board); setBoard(board);
} }
@ -59,19 +60,27 @@ public class BoardPanel extends JPanel {
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);
// Draw the board
g.setColor(Color.white); g.setColor(Color.white);
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) { for (int j = 0; j < 8; j++) {
if (j > 0) g.setColor(g.getColor().equals(Color.white) ? Color.black : Color.white); if (j > 0) g.setColor(g.getColor().equals(Color.white) ? Color.lightGray : Color.white);
g.fillRect(tileSize * i, tileSize * j, tileSize, tileSize); g.fillRect(tileSize * i, tileSize * j, tileSize, tileSize);
} }
// Draw the pieces if a board is present
if (board != null) for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
if (board.getBoardArr()[i][j] != null)
g.drawImage(getPieceTexture(board.getBoardArr()[i][j]), i * tileSize, j * tileSize, this);
} }
private static void loadPieceTextures() { /**
/*
* Load every PNG file inside the res/pieces directory. * Load every PNG file inside the res/pieces directory.
* The filenames without extensions are used as keys in the map. * The filenames without extensions are used as keys in the map textures.
*/ */
private static void loadPieceTextures() {
textures = new HashMap<>(); textures = new HashMap<>();
File dir = new File("res/pieces"); File dir = new File("res/pieces");
File[] files = dir.listFiles((File parentDir, String name) -> name.toLowerCase().endsWith(".png")); File[] files = dir.listFiles((File parentDir, String name) -> name.toLowerCase().endsWith(".png"));
@ -79,6 +88,17 @@ public class BoardPanel extends JPanel {
textures.put(file.getName().replaceFirst("[.][^.]+$", ""), TextureLoader.loadScaledImage(file, tileSize)); textures.put(file.getName().replaceFirst("[.][^.]+$", ""), TextureLoader.loadScaledImage(file, tileSize));
} }
/**
* Loads a piece texture fitting to a piece object
*
* @param piece The piece from which the texture properties are taken
* @return The fitting texture
*/
private static Image getPieceTexture(Piece piece) {
String key = piece.getType().toString().toLowerCase() + "_" + piece.getColor().toString().toLowerCase();
return textures.get(key);
}
@Override @Override
public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMinimumSize() { return getPreferredSize(); }

View File

@ -48,7 +48,7 @@ public class Chess {
mframe.setBounds(100, 100, 740, 740); mframe.setBounds(100, 100, 740, 740);
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoardPanel boardPanel = new BoardPanel(); BoardPanel boardPanel = new BoardPanel(new Board());
boardPanel.setLayout(null); boardPanel.setLayout(null);
mframe.getContentPane().add(boardPanel, BorderLayout.CENTER); mframe.getContentPane().add(boardPanel, BorderLayout.CENTER);
} }

View File

@ -0,0 +1,17 @@
package dev.kske.chess;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Pawn.java</strong><br>
* Created: <strong>01.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class Pawn extends Piece {
public Pawn(Color color) {
super(color);
}
@Override
public Type getType() { return Type.PAWN; }
}

View File

@ -8,9 +8,21 @@ package dev.kske.chess;
*/ */
public abstract class Piece { public abstract class Piece {
private Color color;
public Piece(Color color) {
this.color = color;
}
public abstract Type getType(); public abstract Type getType();
public Color getColor() { return color; }
public static enum Type { public static enum Type {
KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN; KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN;
} }
public static enum Color {
WHITE, BLACK;
}
} }