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"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<classpath>
|
<classpath>
|
||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="res"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||||
<attributes>
|
<attributes>
|
||||||
<attribute name="module" value="true"/>
|
<attribute name="module" value="true"/>
|
||||||
</attributes>
|
</attributes>
|
||||||
</classpathentry>
|
</classpathentry>
|
||||||
<classpathentry kind="lib" path="res"/>
|
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
@ -2,8 +2,8 @@ package dev.kske.chess.ui;
|
|||||||
|
|
||||||
import java.awt.Image;
|
import java.awt.Image;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -19,11 +19,13 @@ import dev.kske.chess.board.Piece;
|
|||||||
*/
|
*/
|
||||||
public class TextureUtil {
|
public class TextureUtil {
|
||||||
|
|
||||||
private static Map<String, Image> textures;
|
private static Map<String, Image> textures, scaledTextures;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
textures = new HashMap<>();
|
textures = new HashMap<>();
|
||||||
|
scaledTextures = new HashMap<>();
|
||||||
loadPieceTextures();
|
loadPieceTextures();
|
||||||
|
scaledTextures.putAll(textures);
|
||||||
}
|
}
|
||||||
|
|
||||||
private TextureUtil() {}
|
private TextureUtil() {}
|
||||||
@ -36,26 +38,28 @@ public class TextureUtil {
|
|||||||
*/
|
*/
|
||||||
public static Image getPieceTexture(Piece piece) {
|
public static Image getPieceTexture(Piece piece) {
|
||||||
String key = piece.getType().toString().toLowerCase() + "_" + piece.getColor().toString().toLowerCase();
|
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
|
* Scales all piece textures to fit the current tile size
|
||||||
*/
|
*/
|
||||||
public static void scalePieceTextures(int scale) {
|
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
|
* @return The loaded image
|
||||||
*/
|
*/
|
||||||
private static Image loadImage(File file) {
|
private static Image loadImage(String fileName) {
|
||||||
BufferedImage in = null;
|
BufferedImage in = null;
|
||||||
try {
|
try {
|
||||||
in = ImageIO.read(file);
|
in = ImageIO.read(TextureUtil.class.getResourceAsStream(fileName));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -67,10 +71,20 @@ public class TextureUtil {
|
|||||||
* The filenames without extensions are used as keys in the map textures.
|
* The filenames without extensions are used as keys in the map textures.
|
||||||
*/
|
*/
|
||||||
private static void loadPieceTextures() {
|
private static void loadPieceTextures() {
|
||||||
File dir = new File("res/pieces");
|
Arrays
|
||||||
File[] files = dir.listFiles((File parentDir, String name) -> name.toLowerCase().endsWith(".png"));
|
.asList("king_white",
|
||||||
for (File file : files)
|
"king_black",
|
||||||
textures.put(file.getName().replaceFirst("[.][^.]+$", ""), TextureUtil.loadImage(file));
|
"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