-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoardPanel.java
More file actions
327 lines (278 loc) · 12.4 KB
/
BoardPanel.java
File metadata and controls
327 lines (278 loc) · 12.4 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.util.List;
/**
* A beautifully rendered game board with cells for ships and shots.
*/
public class BoardPanel extends JPanel {
private GRID grid;
private int boardSize;
private boolean isOpponentView;
private boolean isInteractive;
private CellClickListener clickListener;
private int hoveredRow = -1;
private int hoveredCol = -1;
// Ship placement mode
private boolean placementMode = false;
private int placementLength = 0;
private Orientation placementOrientation = Orientation.HORIZONTAL;
// Cell rendering
private static final int CELL_SIZE = 40;
private static final int CELL_GAP = 2;
private static final int LABEL_SIZE = 25;
private static final int CORNER_RADIUS = 6;
public interface CellClickListener {
void onCellClicked(int row, int col);
}
public BoardPanel(int boardSize, boolean isOpponentView) {
this.boardSize = boardSize;
this.isOpponentView = isOpponentView;
this.isInteractive = false;
int totalSize = LABEL_SIZE + boardSize * (CELL_SIZE + CELL_GAP) + 20;
setPreferredSize(new Dimension(totalSize, totalSize));
setMinimumSize(new Dimension(totalSize, totalSize));
setOpaque(false);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (isInteractive && clickListener != null) {
int[] cell = getCellAt(e.getX(), e.getY());
if (cell != null) {
clickListener.onCellClicked(cell[0], cell[1]);
}
}
}
@Override
public void mouseExited(MouseEvent e) {
hoveredRow = -1;
hoveredCol = -1;
repaint();
}
});
addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int[] cell = getCellAt(e.getX(), e.getY());
if (cell != null) {
if (hoveredRow != cell[0] || hoveredCol != cell[1]) {
hoveredRow = cell[0];
hoveredCol = cell[1];
repaint();
}
} else {
if (hoveredRow != -1) {
hoveredRow = -1;
hoveredCol = -1;
repaint();
}
}
}
});
}
private int[] getCellAt(int x, int y) {
int startX = LABEL_SIZE + 10;
int startY = LABEL_SIZE + 10;
int col = (x - startX) / (CELL_SIZE + CELL_GAP);
int row = (y - startY) / (CELL_SIZE + CELL_GAP);
if (row >= 0 && row < boardSize && col >= 0 && col < boardSize) {
int cellX = startX + col * (CELL_SIZE + CELL_GAP);
int cellY = startY + row * (CELL_SIZE + CELL_GAP);
if (x >= cellX && x < cellX + CELL_SIZE && y >= cellY && y < cellY + CELL_SIZE) {
return new int[]{row, col};
}
}
return null;
}
public void setGrid(GRID grid) {
this.grid = grid;
repaint();
}
public void setInteractive(boolean interactive) {
this.isInteractive = interactive;
setCursor(interactive ? new Cursor(Cursor.HAND_CURSOR) : new Cursor(Cursor.DEFAULT_CURSOR));
}
public void setClickListener(CellClickListener listener) {
this.clickListener = listener;
}
public void setPlacementMode(boolean enabled, int length, Orientation orientation) {
this.placementMode = enabled;
this.placementLength = length;
this.placementOrientation = orientation;
repaint();
}
public void setPlacementOrientation(Orientation orientation) {
this.placementOrientation = orientation;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int startX = LABEL_SIZE + 10;
int startY = LABEL_SIZE + 10;
// Draw column labels (1-10)
g2d.setFont(BattleshipGUI.SMALL_FONT.deriveFont(Font.BOLD));
g2d.setColor(BattleshipGUI.TEXT_SECONDARY);
for (int col = 0; col < boardSize; col++) {
String label = String.valueOf(col + 1);
FontMetrics fm = g2d.getFontMetrics();
int labelX = startX + col * (CELL_SIZE + CELL_GAP) + (CELL_SIZE - fm.stringWidth(label)) / 2;
g2d.drawString(label, labelX, startY - 8);
}
// Draw row labels (A-J)
for (int row = 0; row < boardSize; row++) {
String label = String.valueOf((char)('A' + row));
FontMetrics fm = g2d.getFontMetrics();
int labelY = startY + row * (CELL_SIZE + CELL_GAP) + (CELL_SIZE + fm.getAscent()) / 2 - 2;
g2d.drawString(label, 5, labelY);
}
// Draw cells
for (int row = 0; row < boardSize; row++) {
for (int col = 0; col < boardSize; col++) {
drawCell(g2d, row, col, startX, startY);
}
}
// Draw placement preview
if (placementMode && hoveredRow >= 0 && hoveredCol >= 0) {
drawPlacementPreview(g2d, startX, startY);
}
g2d.dispose();
}
private void drawCell(Graphics2D g2d, int row, int col, int startX, int startY) {
int x = startX + col * (CELL_SIZE + CELL_GAP);
int y = startY + row * (CELL_SIZE + CELL_GAP);
boolean isHovered = (row == hoveredRow && col == hoveredCol && isInteractive);
boolean hasShip = false;
boolean wasShot = false;
boolean isSunk = false;
if (grid != null) {
Coordinate coord = new Coordinate(row, col);
hasShip = grid.hasShipAt(coord);
wasShot = grid.hasShotAt(coord);
// Check if ship at this cell is sunk
if (hasShip && wasShot) {
for (SHIP ship : grid.getShips()) {
if (ship.occupies(coord) && ship.isSunk()) {
isSunk = true;
break;
}
}
}
}
Color cellColor;
Color borderColor;
if (wasShot && hasShip) {
// Hit
cellColor = isSunk ? BattleshipGUI.SUNK_COLOR : BattleshipGUI.HIT_COLOR;
borderColor = BattleshipGUI.HIT_GLOW;
} else if (wasShot) {
// Miss
cellColor = BattleshipGUI.MISS_COLOR;
borderColor = BattleshipGUI.MISS_COLOR.brighter();
} else if (!isOpponentView && hasShip) {
// Own ship (visible)
cellColor = isHovered ? BattleshipGUI.SHIP_HOVER : BattleshipGUI.SHIP_COLOR;
borderColor = BattleshipGUI.ACCENT_COLOR;
} else {
// Water
cellColor = isHovered ? BattleshipGUI.WATER_HOVER : BattleshipGUI.WATER_COLOR;
borderColor = BattleshipGUI.OCEAN_LIGHT;
}
// Draw cell shadow
g2d.setColor(new Color(0, 0, 0, 30));
g2d.fill(new RoundRectangle2D.Float(x + 2, y + 2, CELL_SIZE, CELL_SIZE, CORNER_RADIUS, CORNER_RADIUS));
// Draw cell background with gradient
GradientPaint gp = new GradientPaint(x, y, cellColor, x, y + CELL_SIZE, darker(cellColor, 0.15f));
g2d.setPaint(gp);
g2d.fill(new RoundRectangle2D.Float(x, y, CELL_SIZE, CELL_SIZE, CORNER_RADIUS, CORNER_RADIUS));
// Draw cell border
g2d.setColor(borderColor);
g2d.setStroke(new BasicStroke(1.5f));
g2d.draw(new RoundRectangle2D.Float(x + 0.5f, y + 0.5f, CELL_SIZE - 1, CELL_SIZE - 1, CORNER_RADIUS, CORNER_RADIUS));
// Draw icons for hits/misses
if (wasShot && hasShip) {
// Draw X for hit
drawHitMarker(g2d, x, y);
} else if (wasShot) {
// Draw dot for miss
drawMissMarker(g2d, x, y);
} else if (!isOpponentView && hasShip) {
// Draw ship indicator
drawShipIndicator(g2d, x, y);
}
// Hover glow effect
if (isHovered && isInteractive && !wasShot) {
g2d.setColor(new Color(255, 255, 255, 40));
g2d.fill(new RoundRectangle2D.Float(x, y, CELL_SIZE, CELL_SIZE / 2, CORNER_RADIUS, CORNER_RADIUS));
}
}
private void drawHitMarker(Graphics2D g2d, int x, int y) {
int padding = 10;
g2d.setColor(Color.WHITE);
g2d.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(x + padding, y + padding, x + CELL_SIZE - padding, y + CELL_SIZE - padding);
g2d.drawLine(x + CELL_SIZE - padding, y + padding, x + padding, y + CELL_SIZE - padding);
// Glow effect
g2d.setColor(new Color(255, 100, 100, 100));
g2d.setStroke(new BasicStroke(5, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
g2d.drawLine(x + padding, y + padding, x + CELL_SIZE - padding, y + CELL_SIZE - padding);
g2d.drawLine(x + CELL_SIZE - padding, y + padding, x + padding, y + CELL_SIZE - padding);
}
private void drawMissMarker(Graphics2D g2d, int x, int y) {
int centerX = x + CELL_SIZE / 2;
int centerY = y + CELL_SIZE / 2;
int radius = 6;
g2d.setColor(new Color(255, 255, 255, 180));
g2d.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
}
private void drawShipIndicator(Graphics2D g2d, int x, int y) {
g2d.setColor(new Color(255, 255, 255, 70));
int cx = x + CELL_SIZE / 2;
int cy = y + CELL_SIZE / 2;
int r = 7;
g2d.setStroke(new BasicStroke(2f));
g2d.drawOval(cx - r, cy - r, 2 * r, 2 * r);
g2d.drawLine(cx - r + 2, cy + r + 2, cx + r - 2, cy - r - 2);
}
private void drawPlacementPreview(Graphics2D g2d, int startX, int startY) {
boolean canPlace = true;
// Check if placement is valid
if (grid != null) {
try {
Coordinate start = new Coordinate(hoveredRow, hoveredCol);
canPlace = grid.canPlaceShip(start, placementOrientation, placementLength);
} catch (Exception e) {
canPlace = false;
}
}
Color previewColor = canPlace ?
new Color(BattleshipGUI.SUCCESS_COLOR.getRed(), BattleshipGUI.SUCCESS_COLOR.getGreen(),
BattleshipGUI.SUCCESS_COLOR.getBlue(), 150) :
new Color(BattleshipGUI.HIT_COLOR.getRed(), BattleshipGUI.HIT_COLOR.getGreen(),
BattleshipGUI.HIT_COLOR.getBlue(), 150);
for (int i = 0; i < placementLength; i++) {
int row = hoveredRow + (placementOrientation == Orientation.VERTICAL ? i : 0);
int col = hoveredCol + (placementOrientation == Orientation.HORIZONTAL ? i : 0);
if (row >= 0 && row < boardSize && col >= 0 && col < boardSize) {
int x = startX + col * (CELL_SIZE + CELL_GAP);
int y = startY + row * (CELL_SIZE + CELL_GAP);
g2d.setColor(previewColor);
g2d.fill(new RoundRectangle2D.Float(x, y, CELL_SIZE, CELL_SIZE, CORNER_RADIUS, CORNER_RADIUS));
g2d.setColor(canPlace ? BattleshipGUI.SUCCESS_COLOR : BattleshipGUI.HIT_COLOR);
g2d.setStroke(new BasicStroke(2));
g2d.draw(new RoundRectangle2D.Float(x, y, CELL_SIZE, CELL_SIZE, CORNER_RADIUS, CORNER_RADIUS));
}
}
}
private Color darker(Color c, float factor) {
return new Color(
Math.max(0, (int)(c.getRed() * (1 - factor))),
Math.max(0, (int)(c.getGreen() * (1 - factor))),
Math.max(0, (int)(c.getBlue() * (1 - factor)))
);
}
}