-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPiecesCanvas.java
More file actions
60 lines (48 loc) · 1.95 KB
/
PiecesCanvas.java
File metadata and controls
60 lines (48 loc) · 1.95 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
package ChessBot;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.awt.*;
import java.awt.image.BufferedImage;
public class PiecesCanvas extends Canvas {
private Grid grid;
private final int boardSize = 800;
private final int squareSize = boardSize / 8;
private final String[] imagesPaths = {"white-pawn.png", "white-bishop.png", "white-knight.png", "white-rook.png", "white-queen.png", "white-king.png", "black-pawn.png", "black-bishop.png", "black-knight.png", "black-rook.png", "black-queen.png", "black-king.png"};
private HashMap<Integer, String> pieceImages;
private List<Image> pieceImageObjects;
private final static Color color1 = new Color(119, 149, 86);
private final static Color color2 = new Color(235, 236, 208);
public PiecesCanvas(Grid myGrid) {
grid = myGrid;
initConstants();
}
private void initConstants() {
pieceImages = new HashMap<>();
for (int i=0; i<imagesPaths.length; i++) {
pieceImages.put(Grid.fenPieceValues[i], "Images/" + imagesPaths[i]);
}
}
public void repaint(List<Rectangle> rects) {
int i = 0;
final Graphics graphics = this.getGraphics();
for (Rectangle rect : rects) {
graphics.setColor((((i / 8) % 2) ^ (i % 2)) == 1 ? color1 : color2);
graphics.fillRect(rect.x, rect.y, rect.width, rect.height);
i++;
}
int piece;
BufferedImage image;
pieceImageObjects = new ArrayList<>();
for (int j=0; j<8; j++) {
for (int k=0; k<8; k++) {
piece = grid.getGridValues()[j * 8 + k];
if (!(piece == 0)) {
image = GUI.generateImage(pieceImages.get(piece));
pieceImageObjects.add(image);
getGraphics().drawImage(image, k * squareSize, j * squareSize, null);
}
}
}
}
}