From b12c43fc8f6d555789b3749523fe3539b0f317e4 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 1 Jul 2019 19:37:15 +0200 Subject: [PATCH] Added pawn rendering + Initializing pawns in Board + Piece rendering in BoardPanel - Fixed size initialization bug in BoardPanel + Pawn class + Color property in Piece --- src/dev/kske/chess/Board.java | 9 ++++++++- src/dev/kske/chess/BoardPanel.java | 30 +++++++++++++++++++++++++----- src/dev/kske/chess/Chess.java | 2 +- src/dev/kske/chess/Pawn.java | 17 +++++++++++++++++ src/dev/kske/chess/Piece.java | 12 ++++++++++++ 5 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 src/dev/kske/chess/Pawn.java diff --git a/src/dev/kske/chess/Board.java b/src/dev/kske/chess/Board.java index 42b6393..7f5821c 100644 --- a/src/dev/kske/chess/Board.java +++ b/src/dev/kske/chess/Board.java @@ -1,5 +1,7 @@ package dev.kske.chess; +import dev.kske.chess.Piece.Color; + /** * Project: Chess
* File: Board.java
@@ -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); + } } /** diff --git a/src/dev/kske/chess/BoardPanel.java b/src/dev/kske/chess/BoardPanel.java index 772f8e5..0fe5877 100644 --- a/src/dev/kske/chess/BoardPanel.java +++ b/src/dev/kske/chess/BoardPanel.java @@ -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); } + /** + * Load every PNG file inside the res/pieces directory. + * The filenames without extensions are used as keys in the map textures. + */ private static void loadPieceTextures() { - /* - * Load every PNG file inside the res/pieces directory. - * The filenames without extensions are used as keys in the map. - */ 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(); } diff --git a/src/dev/kske/chess/Chess.java b/src/dev/kske/chess/Chess.java index 053c1dd..2848563 100644 --- a/src/dev/kske/chess/Chess.java +++ b/src/dev/kske/chess/Chess.java @@ -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); } diff --git a/src/dev/kske/chess/Pawn.java b/src/dev/kske/chess/Pawn.java new file mode 100644 index 0000000..35de96b --- /dev/null +++ b/src/dev/kske/chess/Pawn.java @@ -0,0 +1,17 @@ +package dev.kske.chess; + +/** + * Project: Chess
+ * File: Pawn.java
+ * Created: 01.07.2019
+ * Author: Kai S. K. Engelbart + */ +public class Pawn extends Piece { + + public Pawn(Color color) { + super(color); + } + + @Override + public Type getType() { return Type.PAWN; } +} diff --git a/src/dev/kske/chess/Piece.java b/src/dev/kske/chess/Piece.java index c968805..36e9ca3 100644 --- a/src/dev/kske/chess/Piece.java +++ b/src/dev/kske/chess/Piece.java @@ -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; + } }