-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardWidget.java
More file actions
241 lines (201 loc) · 7.72 KB
/
Copy pathBoardWidget.java
File metadata and controls
241 lines (201 loc) · 7.72 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package amazons;
import ucb.gui2.Pad;
import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.ArrayBlockingQueue;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import static amazons.Piece.*;
import static amazons.Square.sq;
import static amazons.Move.mv;
/** A widget that displays an Amazons game.
* @author Khang
*/
class BoardWidget extends Pad {
/* Parameters controlling sizes, speeds, colors, and fonts. */
/** 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",
ARROW = "arrow.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));
_arrow = ImageIO.read(Utils.getResource(ARROW));
} 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(DARK_SQUARE_COLOR);
g.fillRect(0, 0, BOARD_SIDE, BOARD_SIDE);
for (int i = 0; i < BOARD_SIDE; i += SQUARE_SIDE * 2) {
for (int j = 0; j < BOARD_SIDE; j += SQUARE_SIDE * 2) {
g.setColor(LIGHT_SQUARE_COLOR);
g.fillRect(i, j, SQUARE_SIDE, SQUARE_SIDE);
}
}
for (int i = SQUARE_SIDE; i < BOARD_SIDE; i += SQUARE_SIDE * 2) {
for (int j = SQUARE_SIDE; j < BOARD_SIDE; j += SQUARE_SIDE * 2) {
g.setColor(LIGHT_SQUARE_COLOR);
g.fillRect(i, j, SQUARE_SIDE, SQUARE_SIDE);
}
}
if (_selectedFrom != null) {
g.setColor(Color.GREEN);
g.fillRect(cx(_selectedFrom.col()),
cy(_selectedFrom.row()), SQUARE_SIDE, SQUARE_SIDE);
if (_selectedTo != null) {
g.setColor(Color.GREEN);
g.fillRect(cx(_selectedTo.col()),
cy(_selectedTo.row()), SQUARE_SIDE, SQUARE_SIDE);
}
}
try {
_blackQueen = ImageIO.read
(getClass().getResourceAsStream("bq4.png"));
} catch (IOException e) {
e.printStackTrace();
}
try {
_whiteQueen = ImageIO.read
(getClass().getResourceAsStream("wq4.png"));
} catch (IOException e) {
e.printStackTrace();
}
for (int col = 0; col < _board.SIZE; col++) {
for (int row = 0; row < _board.SIZE; row++) {
if (_board.get(col, row) == BLACK) {
g.drawImage(_blackQueen, SQUARE_SIDE * (col),
SQUARE_SIDE * (Board.SIZE - 1 - row), null);
} else if (_board.get(col, row) == WHITE) {
g.drawImage(_whiteQueen, SQUARE_SIDE * (col),
SQUARE_SIDE * (Board.SIZE - 1 - row), null);
} else if (_board.get(col, row) == SPEAR) {
g.drawImage(_arrow, SQUARE_SIDE * (col),
SQUARE_SIDE * (Board.SIZE - 1 - row), null);
}
}
}
}
@Override
public synchronized void paintComponent(Graphics2D g) {
drawGrid(g);
if (_moveList.size() == 3) {
Polygon arrowHead = new Polygon();
drawQueen(g, _moveList.get(1), _board.get(_moveList.get(0)));
g.drawImage(_arrow, cx(_moveList.get(2).col()),
cy(_moveList.get(2).row()), null);
_acceptingMoves = true;
_moveList.clear();
}
}
/** 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) {
if (_selectedFrom == null && _selectedTo == null) {
if (_board.isLegal(s)) {
_selectedFrom = s;
}
} else if (_selectedFrom != null && _selectedTo == null) {
if (_board.isLegal(_selectedFrom, s)) {
_selectedTo = s;
} else {
_selectedFrom = null;
}
} else if (_selectedFrom != null && _selectedTo != null) {
if (_board.isLegal(_selectedFrom, _selectedTo, s)) {
_commands.add(mv(_selectedFrom, _selectedTo, s).toString());
}
_selectedTo = null;
_selectedFrom = null;
}
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;
/** Image of arrow. */
private BufferedImage _arrow;
/** True iff accepting moves from user. */
private boolean _acceptingMoves;
/** Selet Move.*/
private Square _selectedFrom;
/** Select TO.*/
private Square _selectedTo;
/** List stores Move.*/
private ArrayList<Square> _moveList = new ArrayList<Square>();
}