-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetupPanel.java
More file actions
459 lines (378 loc) · 15.9 KB
/
SetupPanel.java
File metadata and controls
459 lines (378 loc) · 15.9 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Ship placement setup screen with interactive board.
*/
public class SetupPanel extends JPanel {
private BattleshipGUI parent;
private BoardPanel boardPanel;
private JLabel titleLabel;
private JLabel instructionLabel;
private JLabel shipInfoLabel;
private JPanel shipListPanel;
private ModernButton rotateButton;
private ModernButton randomButton;
private ModernButton confirmButton;
private SettingsIconButton settingsButton;
private int currentPlayerIndex;
private GRID currentGrid;
private Orientation currentOrientation = Orientation.HORIZONTAL;
private List<ShipType> fleet;
private List<PlacementTarget> placementTargets;
private int currentPlacementIndex;
public SetupPanel(BattleshipGUI parent) {
this.parent = parent;
this.fleet = parent.getFleetDefinition();
this.placementTargets = buildPlacementTargets();
this.currentPlacementIndex = 0;
setLayout(new BorderLayout(20, 20));
setBackground(BattleshipGUI.OCEAN_DARK);
setBorder(BorderFactory.createEmptyBorder(30, 40, 30, 40));
JPanel headerPanel = createHeaderPanel();
add(headerPanel, BorderLayout.NORTH);
JPanel centerPanel = createCenterPanel();
add(centerPanel, BorderLayout.CENTER);
JPanel controlPanel = createControlPanel();
add(controlPanel, BorderLayout.SOUTH);
}
private JPanel createHeaderPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setOpaque(false);
titleLabel = new JLabel("Schiffe platzieren");
titleLabel.setFont(BattleshipGUI.TITLE_FONT);
titleLabel.setForeground(BattleshipGUI.TEXT_PRIMARY);
instructionLabel = new JLabel("Klicke auf das Spielfeld um dein Schiff zu platzieren");
instructionLabel.setFont(BattleshipGUI.LABEL_FONT);
instructionLabel.setForeground(BattleshipGUI.TEXT_SECONDARY);
instructionLabel.setHorizontalAlignment(SwingConstants.CENTER);
settingsButton = new SettingsIconButton();
settingsButton.addActionListener(e -> parent.showSettingsDialog());
JPanel textPanel = new JPanel();
textPanel.setLayout(new BoxLayout(textPanel, BoxLayout.Y_AXIS));
textPanel.setOpaque(false);
titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
instructionLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
textPanel.add(titleLabel);
textPanel.add(Box.createRigidArea(new Dimension(0, 10)));
textPanel.add(instructionLabel);
panel.add(textPanel, BorderLayout.CENTER);
panel.add(settingsButton, BorderLayout.EAST);
return panel;
}
private JPanel createCenterPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setOpaque(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(10, 20, 10, 20);
boardPanel = new BoardPanel(parent.getBoardSize(), false);
boardPanel.setInteractive(true);
boardPanel.setClickListener((row, col) -> handleCellClick(row, col));
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(boardPanel, gbc);
JPanel infoPanel = createShipInfoPanel();
gbc.gridx = 1;
gbc.gridy = 0;
panel.add(infoPanel, gbc);
return panel;
}
private JPanel createShipInfoPanel() {
JPanel panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(BattleshipGUI.CARD_BG);
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 20, 20);
g2d.setColor(BattleshipGUI.OCEAN_LIGHT);
g2d.setStroke(new BasicStroke(1.5f));
g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, 20, 20);
g2d.dispose();
}
};
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setOpaque(false);
panel.setBorder(BorderFactory.createEmptyBorder(25, 30, 25, 30));
panel.setPreferredSize(new Dimension(280, 400));
JLabel shipTitle = new JLabel("Aktuelles Schiff");
shipTitle.setFont(BattleshipGUI.HEADER_FONT);
shipTitle.setForeground(BattleshipGUI.TEXT_PRIMARY);
shipTitle.setAlignmentX(Component.LEFT_ALIGNMENT);
shipInfoLabel = new JLabel("Warte...");
shipInfoLabel.setFont(BattleshipGUI.LABEL_FONT);
shipInfoLabel.setForeground(BattleshipGUI.ACCENT_COLOR);
shipInfoLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
rotateButton = new ModernButton("Drehen (R)", BattleshipGUI.OCEAN_LIGHT);
rotateButton.setAlignmentX(Component.LEFT_ALIGNMENT);
rotateButton.addActionListener(e -> rotateShip());
JLabel listTitle = new JLabel("Flotte");
listTitle.setFont(BattleshipGUI.HEADER_FONT);
listTitle.setForeground(BattleshipGUI.TEXT_PRIMARY);
listTitle.setAlignmentX(Component.LEFT_ALIGNMENT);
shipListPanel = new JPanel();
shipListPanel.setLayout(new BoxLayout(shipListPanel, BoxLayout.Y_AXIS));
shipListPanel.setOpaque(false);
shipListPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
panel.add(shipTitle);
panel.add(Box.createRigidArea(new Dimension(0, 10)));
panel.add(shipInfoLabel);
panel.add(Box.createRigidArea(new Dimension(0, 20)));
panel.add(rotateButton);
panel.add(Box.createRigidArea(new Dimension(0, 30)));
panel.add(listTitle);
panel.add(Box.createRigidArea(new Dimension(0, 15)));
panel.add(shipListPanel);
panel.add(Box.createVerticalGlue());
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('r'), "rotate");
getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke('R'), "rotate");
getActionMap().put("rotate", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
rotateShip();
}
});
return panel;
}
private JPanel createControlPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
panel.setOpaque(false);
randomButton = new ModernButton("Zufällig platzieren", BattleshipGUI.WARNING_COLOR);
randomButton.setPreferredSize(new Dimension(260, 52));
randomButton.addActionListener(e -> placeRandomly());
confirmButton = new ModernButton("Bestätigen", BattleshipGUI.SUCCESS_COLOR);
confirmButton.setPreferredSize(new Dimension(220, 52));
confirmButton.setEnabled(false);
confirmButton.addActionListener(e -> confirmSetup());
panel.add(randomButton);
panel.add(confirmButton);
return panel;
}
public void startSetup(int playerIndex) {
this.currentPlayerIndex = playerIndex;
this.currentGrid = parent.getGame().getPlayerGrid(playerIndex);
this.currentOrientation = Orientation.HORIZONTAL;
this.placementTargets = buildPlacementTargets();
this.currentPlacementIndex = 0;
titleLabel.setText("Schiffe platzieren - " + currentGrid.getUsername());
boardPanel.setGrid(currentGrid);
updateShipInfo();
updateShipList();
}
private void updateShipInfo() {
boolean allPlaced = allShipsPlaced();
if (!allPlaced && currentPlacementIndex < placementTargets.size()) {
PlacementTarget target = placementTargets.get(currentPlacementIndex);
shipInfoLabel.setText(String.format("%s (Länge: %d)", target.getShipName(), target.getLength()));
boardPanel.setPlacementMode(true, target.getLength(), currentOrientation);
rotateButton.setEnabled(true);
} else {
shipInfoLabel.setText("Alle Schiffe platziert!");
boardPanel.setPlacementMode(false, 0, currentOrientation);
rotateButton.setEnabled(false);
}
confirmButton.setEnabled(allPlaced);
updateInstructionLabel();
}
private void updateInstructionLabel() {
if (allShipsPlaced()) {
instructionLabel.setText("Klicke ein Schiff zum Verschieben oder bestätige");
return;
}
String orientText = currentOrientation == Orientation.HORIZONTAL ? "Horizontal" : "Vertikal";
instructionLabel.setText("Ausrichtung: " + orientText + " - Klicke zum Platzieren");
}
private void updateShipList() {
shipListPanel.removeAll();
for (int i = 0; i < placementTargets.size(); i++) {
PlacementTarget target = placementTargets.get(i);
boolean isCurrent = (i == currentPlacementIndex && !target.isPlaced());
JLabel shipLabel = new JLabel(String.format("%s %s (x%d)",
target.isPlaced() ? "OK" : (isCurrent ? ">" : "o"),
target.getShipName(),
target.getLength()));
shipLabel.setFont(BattleshipGUI.SMALL_FONT);
shipLabel.setForeground(target.isPlaced() ? BattleshipGUI.SUCCESS_COLOR :
(isCurrent ? BattleshipGUI.ACCENT_COLOR : BattleshipGUI.TEXT_SECONDARY));
shipLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
shipLabel.setBorder(BorderFactory.createEmptyBorder(3, 0, 3, 0));
shipListPanel.add(shipLabel);
}
shipListPanel.revalidate();
shipListPanel.repaint();
}
private void handleCellClick(int row, int col) {
if (currentGrid == null) {
return;
}
Coordinate coordinate = new Coordinate(row, col);
if (allShipsPlaced() && currentGrid.hasShipAt(coordinate)) {
pickUpShipForEditing(coordinate);
return;
}
if (allShipsPlaced() || currentPlacementIndex >= placementTargets.size()) {
return;
}
if (currentGrid.hasShipAt(coordinate)) {
return;
}
placeCurrentShip(coordinate);
}
private void pickUpShipForEditing(Coordinate coordinate) {
SHIP ship = currentGrid.getShipAt(coordinate);
if (ship == null) {
return;
}
int placementIndex = findPlacementIndexByName(ship.getName());
if (placementIndex < 0) {
return;
}
if (currentGrid.removeShip(ship.getName())) {
placementTargets.get(placementIndex).setPlaced(false);
currentPlacementIndex = placementIndex;
boardPanel.repaint();
updateShipInfo();
updateShipList();
}
}
private void placeCurrentShip(Coordinate start) {
PlacementTarget target = placementTargets.get(currentPlacementIndex);
try {
if (currentGrid.canPlaceShip(start, currentOrientation, target.getLength())) {
currentGrid.placeShip(target.getShipName(), start, currentOrientation, target.getLength());
target.setPlaced(true);
currentPlacementIndex = findNextUnplacedIndex();
boardPanel.repaint();
updateShipInfo();
updateShipList();
}
} catch (IllegalArgumentException e) {
// Invalid placement, ignore.
}
}
private void rotateShip() {
if (allShipsPlaced()) {
return;
}
currentOrientation = (currentOrientation == Orientation.HORIZONTAL) ?
Orientation.VERTICAL : Orientation.HORIZONTAL;
boardPanel.setPlacementOrientation(currentOrientation);
updateInstructionLabel();
}
private void placeRandomly() {
long seed = System.nanoTime() + currentPlayerIndex * 97L;
if (currentGrid == null) {
return;
}
if (allShipsPlaced()) {
return;
}
Random random = new Random(seed);
List<PlacementTarget> addedTargets = new ArrayList<PlacementTarget>();
for (PlacementTarget target : placementTargets) {
if (target.isPlaced()) {
continue;
}
boolean placed = currentGrid.placeShipRandomly(target.getShipName(), target.getLength(), random, 10_000);
if (!placed) {
rollbackRandomPlacement(addedTargets);
JOptionPane.showMessageDialog(
this,
"Die restlichen Schiffe konnten auf Basis der aktuellen Platzierung nicht automatisch ergänzt werden.",
"Automatisches Platzieren nicht möglich",
JOptionPane.WARNING_MESSAGE
);
boardPanel.repaint();
updateShipInfo();
updateShipList();
return;
}
target.setPlaced(true);
addedTargets.add(target);
}
currentPlacementIndex = placementTargets.size();
boardPanel.repaint();
updateShipInfo();
updateShipList();
}
private void confirmSetup() {
parent.setupComplete(currentPlayerIndex);
}
public void resetState() {
currentGrid = null;
currentOrientation = Orientation.HORIZONTAL;
placementTargets = buildPlacementTargets();
currentPlacementIndex = 0;
titleLabel.setText("Schiffe platzieren");
instructionLabel.setText("Klicke auf das Spielfeld um dein Schiff zu platzieren");
shipInfoLabel.setText("Warte...");
boardPanel.setGrid(null);
boardPanel.setPlacementMode(false, 0, currentOrientation);
rotateButton.setEnabled(false);
confirmButton.setEnabled(false);
shipListPanel.removeAll();
shipListPanel.revalidate();
shipListPanel.repaint();
}
private List<PlacementTarget> buildPlacementTargets() {
List<PlacementTarget> targets = new ArrayList<PlacementTarget>();
for (ShipType type : fleet) {
for (int shipNumber = 1; shipNumber <= type.getCount(); shipNumber++) {
targets.add(new PlacementTarget(type, shipNumber));
}
}
return targets;
}
private int findNextUnplacedIndex() {
for (int i = 0; i < placementTargets.size(); i++) {
if (!placementTargets.get(i).isPlaced()) {
return i;
}
}
return placementTargets.size();
}
private int findPlacementIndexByName(String shipName) {
for (int i = 0; i < placementTargets.size(); i++) {
if (placementTargets.get(i).getShipName().equals(shipName)) {
return i;
}
}
return -1;
}
private boolean allShipsPlaced() {
return findNextUnplacedIndex() >= placementTargets.size();
}
private void rollbackRandomPlacement(List<PlacementTarget> addedTargets) {
for (PlacementTarget target : addedTargets) {
currentGrid.removeShip(target.getShipName());
target.setPlaced(false);
}
currentPlacementIndex = findNextUnplacedIndex();
}
private static final class PlacementTarget {
private final ShipType shipType;
private final int shipNumber;
private boolean placed;
private PlacementTarget(ShipType shipType, int shipNumber) {
this.shipType = shipType;
this.shipNumber = shipNumber;
this.placed = false;
}
private String getShipName() {
return shipType.getName() + " #" + shipNumber;
}
private int getLength() {
return shipType.getLength();
}
private boolean isPlaced() {
return placed;
}
private void setPlaced(boolean placed) {
this.placed = placed;
}
}
}