feat: asset manager
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package chess.view.simplerender;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -16,24 +17,27 @@ import chess.model.pieces.Knight;
|
||||
import chess.model.pieces.Pawn;
|
||||
import chess.model.pieces.Queen;
|
||||
import chess.model.pieces.Rook;
|
||||
import chess.view.AssetManager;
|
||||
|
||||
public class PieceIcon implements PieceVisitor<String> {
|
||||
|
||||
private static final String basePath = "app/src/main/resources/pieces2D/";
|
||||
private static final String basePath = "pieces2D/";
|
||||
private static final Map<String, Icon> cache = new HashMap<>();
|
||||
|
||||
public Icon getIcon(Piece piece) {
|
||||
public Icon getIcon(Piece piece) throws IOException {
|
||||
if (piece == null)
|
||||
return null;
|
||||
String path = basePath + colorToString(piece.getColor()) + "-" + visit(piece) + ".png";
|
||||
return getIcon(path);
|
||||
}
|
||||
|
||||
private Icon getIcon(String path) {
|
||||
|
||||
private Icon getIcon(String path) throws IOException {
|
||||
Icon image = cache.get(path);
|
||||
if (image != null)
|
||||
return image;
|
||||
image = new ImageIcon(new ImageIcon(path).getImage().getScaledInstance(100,100, Image.SCALE_SMOOTH));
|
||||
|
||||
image = new ImageIcon(new ImageIcon(AssetManager.getResource(path).readAllBytes()).getImage()
|
||||
.getScaledInstance(100, 100, Image.SCALE_SMOOTH));
|
||||
cache.put(path, image);
|
||||
return image;
|
||||
}
|
||||
@@ -71,5 +75,5 @@ public class PieceIcon implements PieceVisitor<String> {
|
||||
public String visitPiece(Rook rook) {
|
||||
return "rook";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user