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
Before Width: | Height: | Size: 1.2 KiB After Width: | Height: | Size: 1.2 KiB |
Before Width: | Height: | Size: 1.9 KiB After Width: | Height: | Size: 1.9 KiB |
Before Width: | Height: | Size: 2.4 KiB After Width: | Height: | Size: 2.4 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.8 KiB After Width: | Height: | Size: 1.8 KiB |
Before Width: | Height: | Size: 797 B After Width: | Height: | Size: 797 B |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
Before Width: | Height: | Size: 2.2 KiB After Width: | Height: | Size: 2.2 KiB |
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
Before Width: | Height: | Size: 725 B After Width: | Height: | Size: 725 B |
Before Width: | Height: | Size: 933 B After Width: | Height: | Size: 933 B |
25
src/dev/kske/chess/Board.java
Normal 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; }
|
||||
}
|
@ -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<String, Image> 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; }
|
||||
}
|
||||
|
16
src/dev/kske/chess/Piece.java
Normal 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;
|
||||
}
|
||||
}
|
@ -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();
|
||||
}
|
||||
|