-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardWidget.java
More file actions
140 lines (114 loc) · 4.17 KB
/
Copy pathBoardWidget.java
File metadata and controls
140 lines (114 loc) · 4.17 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package amazons;
import ucb.gui2.Pad;
import java.io.IOException;
import java.util.concurrent.ArrayBlockingQueue;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import static amazons.Piece.*;
import static amazons.Square.sq;
/** A widget that displays an Amazons game.
* @author JaniceNg
*/
class BoardWidget extends Pad {
/** Colors of empty squares and grid lines. */
static final Color
SPEAR_COLOR = new Color(64, 64, 64),
LIGHT_SQUARE_COLOR = new Color(238, 207, 161),
DARK_SQUARE_COLOR = new Color(205, 133, 63);
/** Locations of images of white and black queens. */
private static final String
WHITE_QUEEN_IMAGE = "wq4.png",
BLACK_QUEEN_IMAGE = "bq4.png";
/** Size parameters. */
private static final int
SQUARE_SIDE = 30,
BOARD_SIDE = SQUARE_SIDE * 10;
/** A graphical representation of an Amazons board that sends commands
* derived from mouse clicks to COMMANDS. */
BoardWidget(ArrayBlockingQueue<String> commands) {
_commands = commands;
setMouseHandler("click", this::mouseClicked);
setPreferredSize(BOARD_SIDE, BOARD_SIDE);
try {
_whiteQueen = ImageIO.read(Utils.getResource(WHITE_QUEEN_IMAGE));
_blackQueen = ImageIO.read(Utils.getResource(BLACK_QUEEN_IMAGE));
} catch (IOException excp) {
System.err.println("Could not read queen images.");
System.exit(1);
}
_acceptingMoves = false;
}
/** Draw the bare board G. */
private void drawGrid(Graphics2D g) {
g.setColor(LIGHT_SQUARE_COLOR);
g.fillRect(0, 0, BOARD_SIDE, BOARD_SIDE);
}
@Override
public synchronized void paintComponent(Graphics2D g) {
drawGrid(g);
}
/** Draw a queen for side PIECE at square S on G. */
private void drawQueen(Graphics2D g, Square s, Piece piece) {
g.drawImage(piece == WHITE ? _whiteQueen : _blackQueen,
cx(s.col()) + 2, cy(s.row()) + 4, null);
}
/** Handle a click on S. */
private void click(Square s) {
repaint();
}
/** Handle mouse click event E. */
private synchronized void mouseClicked(String unused, MouseEvent e) {
int xpos = e.getX(), ypos = e.getY();
int x = xpos / SQUARE_SIDE,
y = (BOARD_SIDE - ypos) / SQUARE_SIDE;
if (_acceptingMoves
&& x >= 0 && x < Board.SIZE && y >= 0 && y < Board.SIZE) {
click(sq(x, y));
}
}
/** Revise the displayed board according to BOARD. */
synchronized void update(Board board) {
_board.copy(board);
repaint();
}
/** Turn on move collection iff COLLECTING, and clear any current
* partial selection. When move collection is off, ignore clicks on
* the board. */
void setMoveCollection(boolean collecting) {
_acceptingMoves = collecting;
repaint();
}
/** Return x-pixel coordinate of the left corners of column X
* relative to the upper-left corner of the board. */
private int cx(int x) {
return x * SQUARE_SIDE;
}
/** Return y-pixel coordinate of the upper corners of row Y
* relative to the upper-left corner of the board. */
private int cy(int y) {
return (Board.SIZE - y - 1) * SQUARE_SIDE;
}
/** Return x-pixel coordinate of the left corner of S
* relative to the upper-left corner of the board. */
private int cx(Square s) {
return cx(s.col());
}
/** Return y-pixel coordinate of the upper corner of S
* relative to the upper-left corner of the board. */
private int cy(Square s) {
return cy(s.row());
}
/** Queue on which to post move commands (from mouse clicks). */
private ArrayBlockingQueue<String> _commands;
/** Board being displayed. */
private final Board _board = new Board();
/** Image of white queen. */
private BufferedImage _whiteQueen;
/** Image of black queen. */
private BufferedImage _blackQueen;
/** True iff accepting moves from user. */
private boolean _acceptingMoves;
}