From 0e99db6a46b12bd79e2aa60864253d25f4cbc426 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 1 Jul 2019 19:08:37 +0200 Subject: [PATCH] Preparing board implementation and rendering + Board class as data model + Texture loading code in BoardPanel + Abstract Piece class with Type enum - Moved piece textures into res/pieces - Changes TextureLoader to work directly with files --- res/{ => pieces}/bishop_black.png | Bin res/{ => pieces}/bishop_white.png | Bin res/{ => pieces}/king_black.png | Bin res/{ => pieces}/king_white.png | Bin res/{ => pieces}/knight_black.png | Bin res/{ => pieces}/knight_white.png | Bin res/{ => pieces}/pawn_black.png | Bin res/{ => pieces}/pawn_white.png | Bin res/{ => pieces}/queen_black.png | Bin res/{ => pieces}/queen_white.png | Bin res/{ => pieces}/rook_black.png | Bin res/{ => pieces}/rook_white.png | Bin src/dev/kske/chess/Board.java | 25 ++++++++++++++++ src/dev/kske/chess/BoardPanel.java | 40 ++++++++++++++++++++++++-- src/dev/kske/chess/Piece.java | 16 +++++++++++ src/dev/kske/chess/TextureLoader.java | 9 +++--- 16 files changed, 83 insertions(+), 7 deletions(-) rename res/{ => pieces}/bishop_black.png (100%) rename res/{ => pieces}/bishop_white.png (100%) rename res/{ => pieces}/king_black.png (100%) rename res/{ => pieces}/king_white.png (100%) rename res/{ => pieces}/knight_black.png (100%) rename res/{ => pieces}/knight_white.png (100%) rename res/{ => pieces}/pawn_black.png (100%) rename res/{ => pieces}/pawn_white.png (100%) rename res/{ => pieces}/queen_black.png (100%) rename res/{ => pieces}/queen_white.png (100%) rename res/{ => pieces}/rook_black.png (100%) rename res/{ => pieces}/rook_white.png (100%) create mode 100644 src/dev/kske/chess/Board.java create mode 100644 src/dev/kske/chess/Piece.java diff --git a/res/bishop_black.png b/res/pieces/bishop_black.png similarity index 100% rename from res/bishop_black.png rename to res/pieces/bishop_black.png diff --git a/res/bishop_white.png b/res/pieces/bishop_white.png similarity index 100% rename from res/bishop_white.png rename to res/pieces/bishop_white.png diff --git a/res/king_black.png b/res/pieces/king_black.png similarity index 100% rename from res/king_black.png rename to res/pieces/king_black.png diff --git a/res/king_white.png b/res/pieces/king_white.png similarity index 100% rename from res/king_white.png rename to res/pieces/king_white.png diff --git a/res/knight_black.png b/res/pieces/knight_black.png similarity index 100% rename from res/knight_black.png rename to res/pieces/knight_black.png diff --git a/res/knight_white.png b/res/pieces/knight_white.png similarity index 100% rename from res/knight_white.png rename to res/pieces/knight_white.png diff --git a/res/pawn_black.png b/res/pieces/pawn_black.png similarity index 100% rename from res/pawn_black.png rename to res/pieces/pawn_black.png diff --git a/res/pawn_white.png b/res/pieces/pawn_white.png similarity index 100% rename from res/pawn_white.png rename to res/pieces/pawn_white.png diff --git a/res/queen_black.png b/res/pieces/queen_black.png similarity index 100% rename from res/queen_black.png rename to res/pieces/queen_black.png diff --git a/res/queen_white.png b/res/pieces/queen_white.png similarity index 100% rename from res/queen_white.png rename to res/pieces/queen_white.png diff --git a/res/rook_black.png b/res/pieces/rook_black.png similarity index 100% rename from res/rook_black.png rename to res/pieces/rook_black.png diff --git a/res/rook_white.png b/res/pieces/rook_white.png similarity index 100% rename from res/rook_white.png rename to res/pieces/rook_white.png diff --git a/src/dev/kske/chess/Board.java b/src/dev/kske/chess/Board.java new file mode 100644 index 0000000..42b6393 --- /dev/null +++ b/src/dev/kske/chess/Board.java @@ -0,0 +1,25 @@ +package dev.kske.chess; + +/** + * Project: Chess
+ * File: Board.java
+ * Created: 01.07.2019
+ * Author: Kai S. K. Engelbart + */ +public class Board { + + private Piece[][] boardArr; + + public Board() { + initializeDefaultPositions(); + } + + private void initializeDefaultPositions() { + + } + + /** + * @return the board array + */ + public Piece[][] getBoardArr() { return boardArr; } +} diff --git a/src/dev/kske/chess/BoardPanel.java b/src/dev/kske/chess/BoardPanel.java index 3cc007e..772f8e5 100644 --- a/src/dev/kske/chess/BoardPanel.java +++ b/src/dev/kske/chess/BoardPanel.java @@ -4,8 +4,12 @@ import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Graphics; +import java.awt.Image; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; +import java.io.File; +import java.util.HashMap; +import java.util.Map; import javax.swing.JPanel; @@ -23,15 +27,31 @@ public class BoardPanel extends JPanel { private static final long serialVersionUID = 6771148331334310216L; - private int tileSize; + private static int tileSize; + + private static Map textures; + + private Board board; + + public BoardPanel(Board board) { + this(); + setBoard(board); + } public BoardPanel() { - // Add a component listener for adjusting the tile size on resizing + /* + * Add a component listener for adjusting the tile size on resizing. + * The size of the board is assumed to be 8x8, as well as the both the board and + * the tiles being square. + */ addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { tileSize = getWidth() / 8; + + // Load the piece textures if they are not present + if (textures == null) loadPieceTextures(); } }); } @@ -47,6 +67,18 @@ public class BoardPanel extends JPanel { } } + 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")); + for (File file : files) + textures.put(file.getName().replaceFirst("[.][^.]+$", ""), TextureLoader.loadScaledImage(file, tileSize)); + } + @Override public Dimension getMinimumSize() { return getPreferredSize(); } @@ -64,4 +96,8 @@ public class BoardPanel extends JPanel { int s = Math.max(w, h); return new Dimension(s, s); } + + public Board getBoard() { return board; } + + public void setBoard(Board board) { this.board = board; } } diff --git a/src/dev/kske/chess/Piece.java b/src/dev/kske/chess/Piece.java new file mode 100644 index 0000000..c968805 --- /dev/null +++ b/src/dev/kske/chess/Piece.java @@ -0,0 +1,16 @@ +package dev.kske.chess; + +/** + * Project: Chess
+ * File: Piece.java
+ * Created: 01.07.2019
+ * Author: Kai S. K. Engelbart + */ +public abstract class Piece { + + public abstract Type getType(); + + public static enum Type { + KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN; + } +} diff --git a/src/dev/kske/chess/TextureLoader.java b/src/dev/kske/chess/TextureLoader.java index f805cc6..c690291 100644 --- a/src/dev/kske/chess/TextureLoader.java +++ b/src/dev/kske/chess/TextureLoader.java @@ -18,17 +18,16 @@ public class TextureLoader { private TextureLoader() {} /** - * Loads an image from the resource folder and scales it to a square. + * Loads an image from a file and scales it to a square. * - * @param name The name of the file without the PNG extension in the resource - * folder + * @param file The image file * @param scale The side length of the square to which the image will be scaled * @return The scaled image */ - public static Image loadScaledImage(String name, int scale) { + public static Image loadScaledImage(File file, int scale) { BufferedImage in = null; try { - in = ImageIO.read(new File("res" + File.separator + name + ".png")); + in = ImageIO.read(file); } catch (IOException e) { e.printStackTrace(); }