-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChessPieceSprite.java
More file actions
105 lines (88 loc) · 2.69 KB
/
ChessPieceSprite.java
File metadata and controls
105 lines (88 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package board;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
/**
* A class to maintain ChessPiece Images.
* <p>
* this class load Sprite atlas and generate BufferedImage for each chess piece
*
* @see ChessPieceSpriteType
* @see BufferedImage
* @author LeNerd
* @since 2018-05-23
*/
public class ChessPieceSprite {
/**
* This represents possible chess pieces.
*
* @author LeNerd
*
*/
public enum ChessPieceSpriteType {
BLACK_KING(5, 0), BLACK_QUEEN(4, 0), BLACK_LOOK(3, 0), BLACK_KNIGHT(1, 0), BLACK_BISHOP(2, 0), BLACK_PAWN(0, 0),
WHITE_KING(5, 1), WHITE_QUEEN(4, 1), WHITE_LOOK(3, 1), WHITE_KNIGHT(1, 1), WHITE_BISHOP(2, 1), WHITE_PAWN(0, 1),
RED_KING(5, 5), RED_QUEEN(4, 5), RED_LOOK(3, 5), RED_KNIGHT(1, 5), RED_BISHOP(2, 5), RED_PAWN(0, 5),
GREEN_KING(5, 8), GREEN_QUEEN(4, 8), GREEN_LOOK(3, 8), GREEN_KNIGHT(1, 8), GREEN_BISHOP(2, 8), GREEN_PAWN(0, 8);
int x;
int y;
private ChessPieceSpriteType(int x, int y) {
this.x = x;
this.y = y;
}
public String getKey() {
return x + "," + y;
}
}
private static ChessPieceSprite instance = new ChessPieceSprite();
BufferedImage imgAtlas;
Map<String, BufferedImage> sprites;
ChessPieceSprite() {
try {
imgAtlas = ImageIO.read(new File("sprite2.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sprites = new HashMap<>();
}
/**
* returns current instance of ChessPieceSprite
*
* @return instance of ChessPieceSprite
*/
public static ChessPieceSprite getInstace() {
return instance;
}
/**
* returns BufferedImage for given ChessPieceSpriteType
* <p>
* this function generates BufferedImage for first time for each
* ChessPieceSpriteType. any subsequent call is cached.
*
* @param type
* type of chess piece
*
* @return BufferedImage for given ChessPieceSpriteType.
*/
public BufferedImage getChessPiece(ChessPieceSpriteType type) {
if (sprites.get(type.getKey()) == null) {
sprites.put(type.getKey(), getSpriteFromAtlas(type));
}
return sprites.get(type.getKey());
}
private BufferedImage getSpriteFromAtlas(ChessPieceSpriteType type) {
int w = imgAtlas.getWidth() / 6;
int h = imgAtlas.getHeight() / 12;
BufferedImage img = new BufferedImage(w, h, imgAtlas.getType());
Graphics g = img.getGraphics();
g.drawImage(imgAtlas, 0, 0, w, h, type.x * w, type.y * h, type.x * w + w, type.y * h + h, null);
// g.dispose();
return img;
}
}