2019-10-20 17:27:52 +02:00
|
|
|
package dev.kske.chess.io;
|
2019-07-04 14:58:15 +02:00
|
|
|
|
|
|
|
import java.awt.Image;
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
import java.io.IOException;
|
2019-07-15 18:16:45 +02:00
|
|
|
import java.util.Arrays;
|
2019-07-04 14:58:15 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
2019-07-06 18:05:00 +02:00
|
|
|
import dev.kske.chess.board.Piece;
|
2019-07-04 14:58:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {
|
|
|
|
|
2019-09-09 19:05:57 +02:00
|
|
|
private static Map<String, Image> textures = new HashMap<>(), scaledTextures = new HashMap<>();
|
2019-07-04 14:58:15 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
loadPieceTextures();
|
2019-07-15 18:16:45 +02:00
|
|
|
scaledTextures.putAll(textures);
|
2019-07-04 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private TextureUtil() {}
|
|
|
|
|
|
|
|
/**
|
2019-10-13 14:37:35 +02:00
|
|
|
* Loads a piece texture fitting to a piece object.
|
2019-07-04 14:58:15 +02:00
|
|
|
*
|
|
|
|
* @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();
|
2019-07-15 18:16:45 +02:00
|
|
|
return scaledTextures.get(key);
|
2019-07-04 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
2019-10-13 14:37:35 +02:00
|
|
|
|
2019-07-04 14:58:15 +02:00
|
|
|
/**
|
2019-10-13 14:37:35 +02:00
|
|
|
* Scales all piece textures to fit the current tile size.
|
|
|
|
*
|
|
|
|
* @param tileSize the new width and height of the piece textures
|
2019-07-04 14:58:15 +02:00
|
|
|
*/
|
2019-10-13 14:37:35 +02:00
|
|
|
public static void scalePieceTextures(int tileSize) {
|
2019-07-15 18:16:45 +02:00
|
|
|
scaledTextures.clear();
|
|
|
|
textures
|
2019-10-13 14:37:35 +02:00
|
|
|
.forEach((key, img) -> scaledTextures.put(key, img.getScaledInstance(tileSize, tileSize, Image.SCALE_SMOOTH)));
|
2019-07-04 14:58:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-07-15 18:16:45 +02:00
|
|
|
* Loads an image from a file in the resource folder.
|
2019-07-04 14:58:15 +02:00
|
|
|
*
|
2019-07-15 18:16:45 +02:00
|
|
|
* @param fileName The name of the image resource
|
2019-07-04 14:58:15 +02:00
|
|
|
* @return The loaded image
|
|
|
|
*/
|
2019-07-15 18:16:45 +02:00
|
|
|
private static Image loadImage(String fileName) {
|
2019-07-04 14:58:15 +02:00
|
|
|
BufferedImage in = null;
|
|
|
|
try {
|
2019-07-15 18:16:45 +02:00
|
|
|
in = ImageIO.read(TextureUtil.class.getResourceAsStream(fileName));
|
2019-07-04 14:58:15 +02:00
|
|
|
} 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() {
|
2019-07-15 18:16:45 +02:00
|
|
|
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")));
|
2019-07-04 14:58:15 +02:00
|
|
|
}
|
|
|
|
}
|