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
This commit is contained in:
Kai S. K. Engelbart 2019-07-01 19:08:37 +02:00
parent 5300921149
commit 0e99db6a46
16 changed files with 83 additions and 7 deletions

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.9 KiB

After

Width:  |  Height:  |  Size: 1.9 KiB

View File

Before

Width:  |  Height:  |  Size: 2.4 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

View File

Before

Width:  |  Height:  |  Size: 797 B

After

Width:  |  Height:  |  Size: 797 B

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

Before

Width:  |  Height:  |  Size: 2.2 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

Before

Width:  |  Height:  |  Size: 2.6 KiB

After

Width:  |  Height:  |  Size: 2.6 KiB

View File

Before

Width:  |  Height:  |  Size: 725 B

After

Width:  |  Height:  |  Size: 725 B

View File

Before

Width:  |  Height:  |  Size: 933 B

After

Width:  |  Height:  |  Size: 933 B

View File

@ -0,0 +1,25 @@
package dev.kske.chess;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Board.java</strong><br>
* Created: <strong>01.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public class Board {
private Piece[][] boardArr;
public Board() {
initializeDefaultPositions();
}
private void initializeDefaultPositions() {
}
/**
* @return the board array
*/
public Piece[][] getBoardArr() { return boardArr; }
}

View File

@ -4,8 +4,12 @@ import java.awt.Color;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ComponentAdapter; import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent; import java.awt.event.ComponentEvent;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -23,15 +27,31 @@ public class BoardPanel extends JPanel {
private static final long serialVersionUID = 6771148331334310216L; private static final long serialVersionUID = 6771148331334310216L;
private int tileSize; private static int tileSize;
private static Map<String, Image> textures;
private Board board;
public BoardPanel(Board board) {
this();
setBoard(board);
}
public BoardPanel() { 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() { addComponentListener(new ComponentAdapter() {
@Override @Override
public void componentResized(ComponentEvent e) { public void componentResized(ComponentEvent e) {
tileSize = getWidth() / 8; 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 @Override
public Dimension getMinimumSize() { return getPreferredSize(); } public Dimension getMinimumSize() { return getPreferredSize(); }
@ -64,4 +96,8 @@ public class BoardPanel extends JPanel {
int s = Math.max(w, h); int s = Math.max(w, h);
return new Dimension(s, s); return new Dimension(s, s);
} }
public Board getBoard() { return board; }
public void setBoard(Board board) { this.board = board; }
} }

View File

@ -0,0 +1,16 @@
package dev.kske.chess;
/**
* Project: <strong>Chess</strong><br>
* File: <strong>Piece.java</strong><br>
* Created: <strong>01.07.2019</strong><br>
* Author: <strong>Kai S. K. Engelbart</strong>
*/
public abstract class Piece {
public abstract Type getType();
public static enum Type {
KING, QUEEN, ROOK, KNIGHT, BISHOP, PAWN;
}
}

View File

@ -18,17 +18,16 @@ public class TextureLoader {
private 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 * @param file The image file
* folder
* @param scale The side length of the square to which the image will be scaled * @param scale The side length of the square to which the image will be scaled
* @return The scaled image * @return The scaled image
*/ */
public static Image loadScaledImage(String name, int scale) { public static Image loadScaledImage(File file, int scale) {
BufferedImage in = null; BufferedImage in = null;
try { try {
in = ImageIO.read(new File("res" + File.separator + name + ".png")); in = ImageIO.read(file);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }