feat: asset manager
This commit is contained in:
38
app/src/main/java/chess/view/AssetManager.java
Normal file
38
app/src/main/java/chess/view/AssetManager.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package chess.view;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class AssetManager {
|
||||
|
||||
private static final String gradleBase = "app/src/main/resources/";
|
||||
|
||||
public static InputStream getResource(String name) {
|
||||
// we first search it in files
|
||||
InputStream inputStream = getFileInputStream(name);
|
||||
if (inputStream != null)
|
||||
return inputStream;
|
||||
|
||||
inputStream = getFileInputStream(gradleBase + name);
|
||||
if (inputStream != null)
|
||||
return inputStream;
|
||||
// then in the jar
|
||||
return ClassLoader.getSystemResourceAsStream(name);
|
||||
}
|
||||
|
||||
private static InputStream getFileInputStream(String path) {
|
||||
File f = new File(path);
|
||||
if (f.exists()) {
|
||||
FileInputStream fis;
|
||||
try {
|
||||
fis = new FileInputStream(f);
|
||||
return fis;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.awt.Color;
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import javax.swing.JButton;
|
||||
@@ -127,7 +128,11 @@ public class Window extends JFrame implements OutputSystem {
|
||||
for (int y = 0; y < 8; y++) {
|
||||
for (int x = 0; x < 8; x++) {
|
||||
JLabel cell = this.cells[x][y];
|
||||
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
||||
try {
|
||||
cell.setIcon(pieceIcon.getIcon(pieceAt(x, y)));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user