Optimized BoardPanel and texture loading
- Renamed TextureLoader to TextureUtil - Moved entire texture handling to TextureUtil
This commit is contained in:
parent
30074f385e
commit
8b6b9766e2
@ -3,22 +3,17 @@ package dev.kske.chess;
|
|||||||
import java.awt.Color;
|
import java.awt.Color;
|
||||||
import java.awt.Dimension;
|
import java.awt.Dimension;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.Image;
|
|
||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.awt.event.ComponentAdapter;
|
import java.awt.event.ComponentAdapter;
|
||||||
import java.awt.event.ComponentEvent;
|
import java.awt.event.ComponentEvent;
|
||||||
import java.awt.event.MouseAdapter;
|
import java.awt.event.MouseAdapter;
|
||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.io.File;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
import javax.swing.JOptionPane;
|
||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
|
|
||||||
import dev.kske.chess.event.GameEvent;
|
import dev.kske.chess.event.GameEvent;
|
||||||
import dev.kske.chess.event.GameEventListener;
|
import dev.kske.chess.event.GameEventListener;
|
||||||
import dev.kske.chess.piece.Piece;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>Chess</strong><br>
|
* Project: <strong>Chess</strong><br>
|
||||||
@ -34,17 +29,12 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
|||||||
|
|
||||||
private static final long serialVersionUID = 6771148331334310216L;
|
private static final long serialVersionUID = 6771148331334310216L;
|
||||||
|
|
||||||
private static int tileSize;
|
private int tileSize;
|
||||||
|
|
||||||
private static Map<String, Image> textures;
|
|
||||||
|
|
||||||
private Board board;
|
private Board board;
|
||||||
|
|
||||||
public BoardPanel(Board board) {
|
public BoardPanel(Board board) {
|
||||||
this();
|
this();
|
||||||
setSize(getPreferredSize());
|
|
||||||
setBoard(board);
|
setBoard(board);
|
||||||
if (textures == null) loadPieceTextures();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BoardPanel() {
|
public BoardPanel() {
|
||||||
@ -58,10 +48,12 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void componentResized(ComponentEvent e) {
|
public void componentResized(ComponentEvent e) {
|
||||||
tileSize = getWidth() / 8;
|
tileSize = getWidth() / 8;
|
||||||
scalePieceTextures();
|
TextureUtil.scalePieceTextures(tileSize);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
setSize(getPreferredSize());
|
||||||
|
|
||||||
// Add a mouse adapter for testing piece movement
|
// Add a mouse adapter for testing piece movement
|
||||||
addMouseListener(new MouseAdapter() {
|
addMouseListener(new MouseAdapter() {
|
||||||
|
|
||||||
@ -95,8 +87,8 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
|||||||
// Draw the pieces if a board is present
|
// Draw the pieces if a board is present
|
||||||
if (board != null) for (int i = 0; i < 8; i++)
|
if (board != null) for (int i = 0; i < 8; i++)
|
||||||
for (int j = 0; j < 8; j++)
|
for (int j = 0; j < 8; j++)
|
||||||
if (board.getBoardArr()[i][j] != null)
|
if (board.getBoardArr()[i][j] != null) g.drawImage(TextureUtil
|
||||||
g.drawImage(getPieceTexture(board.getBoardArr()[i][j]), i * tileSize, j * tileSize, this);
|
.getPieceTexture(board.getBoardArr()[i][j]), i * tileSize, j * tileSize, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -116,36 +108,6 @@ public class BoardPanel extends JPanel implements GameEventListener {
|
|||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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() {
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Scales all piece textures to fit the current tile size
|
|
||||||
*/
|
|
||||||
private static void scalePieceTextures() {
|
|
||||||
textures.replaceAll((key, img) -> img.getScaledInstance(tileSize, tileSize, Image.SCALE_SMOOTH));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
|
@Override
|
||||||
public Dimension getMinimumSize() { return getPreferredSize(); }
|
public Dimension getMinimumSize() { return getPreferredSize(); }
|
||||||
|
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
package dev.kske.chess;
|
|
||||||
|
|
||||||
import java.awt.Image;
|
|
||||||
import java.awt.image.BufferedImage;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Project: <strong>Chess</strong><br>
|
|
||||||
* File: <strong>TextureLoader.java</strong><br>
|
|
||||||
* Created: <strong>01.07.2019</strong><br>
|
|
||||||
* Author: <strong>Kai S. K. Engelbart</strong>
|
|
||||||
*/
|
|
||||||
public class TextureLoader {
|
|
||||||
|
|
||||||
private TextureLoader() {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads an image from a file.
|
|
||||||
*
|
|
||||||
* @param file The image file
|
|
||||||
* @return The loaded image
|
|
||||||
*/
|
|
||||||
public static Image loadScaledImage(File file, int scale) {
|
|
||||||
BufferedImage in = null;
|
|
||||||
try {
|
|
||||||
in = ImageIO.read(file);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return in;
|
|
||||||
}
|
|
||||||
}
|
|
76
src/dev/kske/chess/TextureUtil.java
Normal file
76
src/dev/kske/chess/TextureUtil.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package dev.kske.chess;
|
||||||
|
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
|
import dev.kske.chess.piece.Piece;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project: <strong>Chess</strong><br>
|
||||||
|
* File: <strong>TextureUtil.java</strong><br>
|
||||||
|
* Created: <strong>01.07.2019</strong><br>
|
||||||
|
* Author: <strong>Kai S. K. Engelbart</strong>
|
||||||
|
*/
|
||||||
|
public class TextureUtil {
|
||||||
|
|
||||||
|
private static Map<String, Image> textures;
|
||||||
|
|
||||||
|
static {
|
||||||
|
textures = new HashMap<>();
|
||||||
|
loadPieceTextures();
|
||||||
|
}
|
||||||
|
|
||||||
|
private TextureUtil() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads a piece texture fitting to a piece object
|
||||||
|
*
|
||||||
|
* @param piece The piece from which the texture properties are taken
|
||||||
|
* @return The fitting texture
|
||||||
|
*/
|
||||||
|
public static Image getPieceTexture(Piece piece) {
|
||||||
|
String key = piece.getType().toString().toLowerCase() + "_" + piece.getColor().toString().toLowerCase();
|
||||||
|
return textures.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scales all piece textures to fit the current tile size
|
||||||
|
*/
|
||||||
|
public static void scalePieceTextures(int scale) {
|
||||||
|
textures.replaceAll((key, img) -> img.getScaledInstance(scale, scale, Image.SCALE_SMOOTH));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads an image from a file.
|
||||||
|
*
|
||||||
|
* @param file The image file
|
||||||
|
* @return The loaded image
|
||||||
|
*/
|
||||||
|
private static Image loadImage(File file) {
|
||||||
|
BufferedImage in = null;
|
||||||
|
try {
|
||||||
|
in = ImageIO.read(file);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
||||||
|
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("[.][^.]+$", ""), TextureUtil.loadImage(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user