From f710b2d6f7666f6cac2be85d942685da73029708 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Thu, 4 Jul 2019 14:58:15 +0200 Subject: [PATCH] Optimized BoardPanel and texture loading - Renamed TextureLoader to TextureUtil - Moved entire texture handling to TextureUtil --- src/dev/kske/chess/BoardPanel.java | 52 +++--------------- src/dev/kske/chess/TextureLoader.java | 35 ------------ src/dev/kske/chess/TextureUtil.java | 76 +++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 80 deletions(-) delete mode 100644 src/dev/kske/chess/TextureLoader.java create mode 100644 src/dev/kske/chess/TextureUtil.java diff --git a/src/dev/kske/chess/BoardPanel.java b/src/dev/kske/chess/BoardPanel.java index 9ccbba0..9237ef8 100644 --- a/src/dev/kske/chess/BoardPanel.java +++ b/src/dev/kske/chess/BoardPanel.java @@ -3,22 +3,17 @@ package dev.kske.chess; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; -import java.awt.Image; import java.awt.Point; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; -import java.io.File; -import java.util.HashMap; -import java.util.Map; import javax.swing.JOptionPane; import javax.swing.JPanel; import dev.kske.chess.event.GameEvent; import dev.kske.chess.event.GameEventListener; -import dev.kske.chess.piece.Piece; /** * Project: Chess
@@ -34,17 +29,12 @@ public class BoardPanel extends JPanel implements GameEventListener { private static final long serialVersionUID = 6771148331334310216L; - private static int tileSize; - - private static Map textures; - - private Board board; + private int tileSize; + private Board board; public BoardPanel(Board board) { this(); - setSize(getPreferredSize()); setBoard(board); - if (textures == null) loadPieceTextures(); } public BoardPanel() { @@ -58,10 +48,12 @@ public class BoardPanel extends JPanel implements GameEventListener { @Override public void componentResized(ComponentEvent e) { tileSize = getWidth() / 8; - scalePieceTextures(); + TextureUtil.scalePieceTextures(tileSize); } }); + setSize(getPreferredSize()); + // Add a mouse adapter for testing piece movement addMouseListener(new MouseAdapter() { @@ -95,8 +87,8 @@ public class BoardPanel extends JPanel implements GameEventListener { // 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); + if (board.getBoardArr()[i][j] != null) g.drawImage(TextureUtil + .getPieceTexture(board.getBoardArr()[i][j]), i * tileSize, j * tileSize, this); } @Override @@ -116,36 +108,6 @@ public class BoardPanel extends JPanel implements GameEventListener { 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 public Dimension getMinimumSize() { return getPreferredSize(); } diff --git a/src/dev/kske/chess/TextureLoader.java b/src/dev/kske/chess/TextureLoader.java deleted file mode 100644 index e8f7ed2..0000000 --- a/src/dev/kske/chess/TextureLoader.java +++ /dev/null @@ -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: Chess
- * File: TextureLoader.java
- * Created: 01.07.2019
- * Author: Kai S. K. Engelbart - */ -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; - } -} diff --git a/src/dev/kske/chess/TextureUtil.java b/src/dev/kske/chess/TextureUtil.java new file mode 100644 index 0000000..cd336f8 --- /dev/null +++ b/src/dev/kske/chess/TextureUtil.java @@ -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: Chess
+ * File: TextureUtil.java
+ * Created: 01.07.2019
+ * Author: Kai S. K. Engelbart + */ +public class TextureUtil { + + private static Map 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)); + } + +}