From 7402653f178821c933db290e15e553179083f6b1 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 15 Jul 2019 18:16:45 +0200 Subject: [PATCH] Added resource folder to class path, implemented proper texture scaling --- .classpath | 2 +- src/dev/kske/chess/ui/TextureUtil.java | 38 ++++++++++++++++++-------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/.classpath b/.classpath index 99d9707..8623669 100644 --- a/.classpath +++ b/.classpath @@ -1,12 +1,12 @@ + - diff --git a/src/dev/kske/chess/ui/TextureUtil.java b/src/dev/kske/chess/ui/TextureUtil.java index 7a37f39..5597032 100644 --- a/src/dev/kske/chess/ui/TextureUtil.java +++ b/src/dev/kske/chess/ui/TextureUtil.java @@ -2,8 +2,8 @@ package dev.kske.chess.ui; import java.awt.Image; import java.awt.image.BufferedImage; -import java.io.File; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -19,11 +19,13 @@ import dev.kske.chess.board.Piece; */ public class TextureUtil { - private static Map textures; + private static Map textures, scaledTextures; static { textures = new HashMap<>(); + scaledTextures = new HashMap<>(); loadPieceTextures(); + scaledTextures.putAll(textures); } private TextureUtil() {} @@ -36,26 +38,28 @@ public class TextureUtil { */ public static Image getPieceTexture(Piece piece) { String key = piece.getType().toString().toLowerCase() + "_" + piece.getColor().toString().toLowerCase(); - return textures.get(key); + return scaledTextures.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)); + scaledTextures.clear(); + textures + .forEach((key, img) -> scaledTextures.put(key, img.getScaledInstance(scale, scale, Image.SCALE_SMOOTH))); } /** - * Loads an image from a file. + * Loads an image from a file in the resource folder. * - * @param file The image file + * @param fileName The name of the image resource * @return The loaded image */ - private static Image loadImage(File file) { + private static Image loadImage(String fileName) { BufferedImage in = null; try { - in = ImageIO.read(file); + in = ImageIO.read(TextureUtil.class.getResourceAsStream(fileName)); } catch (IOException e) { e.printStackTrace(); } @@ -67,10 +71,20 @@ public class TextureUtil { * 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)); + Arrays + .asList("king_white", + "king_black", + "queen_white", + "queen_black", + "rook_white", + "rook_black", + "knight_white", + "knight_black", + "bishop_white", + "bishop_black", + "pawn_white", + "pawn_black") + .forEach(name -> textures.put(name, loadImage("/pieces/" + name + ".png"))); } }