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:
parent
138bda75a4
commit
f420d4f9c2
@ -1,5 +1,7 @@
|
||||
package dev.kske.chess;
|
||||
|
||||
import dev.kske.chess.Piece.Color;
|
||||
|
||||
/**
|
||||
* Project: <strong>Chess</strong><br>
|
||||
* File: <strong>Board.java</strong><br>
|
||||
@ -11,11 +13,16 @@ public class Board {
|
||||
private Piece[][] boardArr;
|
||||
|
||||
public Board() {
|
||||
boardArr = new Piece[8][8];
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -35,6 +35,7 @@ public class BoardPanel extends JPanel {
|
||||
|
||||
public BoardPanel(Board board) {
|
||||
this();
|
||||
setSize(getPreferredSize());
|
||||
setBoard(board);
|
||||
}
|
||||
|
||||
@ -59,19 +60,27 @@ public class BoardPanel extends JPanel {
|
||||
@Override
|
||||
protected void paintComponent(Graphics g) {
|
||||
super.paintComponent(g);
|
||||
|
||||
// Draw the board
|
||||
g.setColor(Color.white);
|
||||
for (int i = 0; i < 8; i++)
|
||||
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);
|
||||
}
|
||||
|
||||
// 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.
|
||||
* 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<>();
|
||||
File dir = new File("res/pieces");
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
public Dimension getMinimumSize() { return getPreferredSize(); }
|
||||
|
||||
|
@ -48,7 +48,7 @@ public class Chess {
|
||||
mframe.setBounds(100, 100, 740, 740);
|
||||
mframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
BoardPanel boardPanel = new BoardPanel();
|
||||
BoardPanel boardPanel = new BoardPanel(new Board());
|
||||
boardPanel.setLayout(null);
|
||||
mframe.getContentPane().add(boardPanel, BorderLayout.CENTER);
|
||||
}
|
||||
|
17
src/dev/kske/chess/Pawn.java
Normal file
17
src/dev/kske/chess/Pawn.java
Normal 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; }
|
||||
}
|
@ -8,9 +8,21 @@ package dev.kske.chess;
|
||||
*/
|
||||
public abstract class Piece {
|
||||
|
||||
private Color color;
|
||||
|
||||
public Piece(Color color) {
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public abstract Type getType();
|
||||
|
||||
public Color getColor() { return color; }
|
||||
|
||||
public static enum Type {
|
||||
KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN;
|
||||
}
|
||||
|
||||
public static enum Color {
|
||||
WHITE, BLACK;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user