Added resource folder to class path, implemented proper texture scaling
This commit is contained in:
parent
5984f4119e
commit
7402653f17
@ -1,12 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="res"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="lib" path="res"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
|
@ -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<String, Image> textures;
|
||||
private static Map<String, Image> 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")));
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user