-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattleshipGUI.java
More file actions
422 lines (360 loc) · 15.7 KB
/
BattleshipGUI.java
File metadata and controls
422 lines (360 loc) · 15.7 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
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
/**
* Main GUI class for Schiffe Versenken (Battleship) game.
* Modern, clean design with smooth animations and beautiful visuals.
*/
public class BattleshipGUI extends JFrame {
// Color scheme - Modern ocean theme
public static final Color OCEAN_DARK = new Color(15, 32, 56);
public static final Color OCEAN_MEDIUM = new Color(22, 54, 92);
public static final Color OCEAN_LIGHT = new Color(32, 78, 135);
public static final Color WATER_COLOR = new Color(47, 109, 176);
public static final Color WATER_HOVER = new Color(64, 133, 198);
public static final Color SHIP_COLOR = new Color(85, 98, 112);
public static final Color SHIP_HOVER = new Color(105, 118, 132);
public static final Color HIT_COLOR = new Color(220, 53, 69);
public static final Color HIT_GLOW = new Color(255, 100, 100);
public static final Color MISS_COLOR = new Color(108, 117, 125);
public static final Color SUNK_COLOR = new Color(139, 0, 0);
public static final Color ACCENT_COLOR = new Color(0, 188, 212);
public static final Color ACCENT_HOVER = new Color(38, 198, 218);
public static final Color SUCCESS_COLOR = new Color(46, 204, 113);
public static final Color WARNING_COLOR = new Color(241, 196, 15);
public static final Color TEXT_PRIMARY = new Color(236, 240, 241);
public static final Color TEXT_SECONDARY = new Color(189, 195, 199);
public static final Color PANEL_BG = new Color(25, 42, 68);
public static final Color CARD_BG = new Color(35, 55, 85);
// Fonts
public static final Font TITLE_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 32);
public static final Font HEADER_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 19);
public static final Font LABEL_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 16);
public static final Font BUTTON_FONT = new Font(Font.SANS_SERIF, Font.BOLD, 16);
public static final Font SMALL_FONT = new Font(Font.SANS_SERIF, Font.PLAIN, 14);
private CardLayout cardLayout;
private JPanel mainPanel;
// Screens
private WelcomePanel welcomePanel;
private SetupPanel setupPanel;
private GamePanel gamePanel;
private ResultPanel resultPanel;
// Game state
private GAME game;
private String player1Name;
private String player2Name;
private List<ShipType> fleetDefinition;
private int boardSize = 10;
public BattleshipGUI() {
setTitle("Schiffe Versenken");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setMinimumSize(new Dimension(1200, 800));
setPreferredSize(new Dimension(1400, 900));
// Setup main card layout
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
mainPanel.setBackground(OCEAN_DARK);
// Initialize fleet
fleetDefinition = defaultFleet();
// Create screens
welcomePanel = new WelcomePanel(this);
setupPanel = new SetupPanel(this);
gamePanel = new GamePanel(this);
resultPanel = new ResultPanel(this);
mainPanel.add(welcomePanel, "welcome");
mainPanel.add(setupPanel, "setup");
mainPanel.add(gamePanel, "game");
mainPanel.add(resultPanel, "result");
add(mainPanel);
// Center on screen
pack();
setLocationRelativeTo(null);
// Start with welcome screen
showWelcome();
}
private List<ShipType> defaultFleet() {
List<ShipType> fleet = new ArrayList<>();
fleet.add(new ShipType("Schlachtschiff", 5, 1));
fleet.add(new ShipType("Zerstörer", 4, 1));
fleet.add(new ShipType("U-Boot", 3, 2));
fleet.add(new ShipType("Patrouillenboot", 2, 1));
return fleet;
}
public void showWelcome() {
cardLayout.show(mainPanel, "welcome");
}
public void startGame(String p1Name, String p2Name) {
this.player1Name = p1Name;
this.player2Name = p2Name;
this.game = new GAME(player1Name, player2Name, boardSize, fleetDefinition);
setupPanel.startSetup(0);
cardLayout.show(mainPanel, "setup");
}
public void setupComplete(int playerIndex) {
if (playerIndex == 0) {
// Show transition, then setup player 2
showTransitionScreen("Spieler 1 Setup komplett!",
"Weiter zu " + player2Name, () -> {
setupPanel.startSetup(1);
});
} else {
// Both players ready, start the game
showTransitionScreen("Beide Spieler bereit!",
"Spiel beginnt...", () -> {
gamePanel.startGame();
cardLayout.show(mainPanel, "game");
});
}
}
private void showTransitionScreen(String message, String subMessage, Runnable onComplete) {
JDialog transition = new JDialog(this, true);
transition.setUndecorated(true);
transition.setBounds(getBounds());
transition.setBackground(OCEAN_DARK);
JPanel overlayPanel = 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);
GradientPaint gp = new GradientPaint(0, 0, OCEAN_DARK, 0, getHeight(), OCEAN_MEDIUM);
g2d.setPaint(gp);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
};
overlayPanel.setLayout(new GridBagLayout());
overlayPanel.setBorder(BorderFactory.createEmptyBorder(60, 60, 60, 60));
JLabel msgLabel = new JLabel(message);
msgLabel.setFont(TITLE_FONT);
msgLabel.setForeground(TEXT_PRIMARY);
msgLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
JLabel subLabel = new JLabel(subMessage);
subLabel.setFont(HEADER_FONT);
subLabel.setForeground(TEXT_SECONDARY);
subLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
ModernButton continueBtn = new ModernButton("Weiter", ACCENT_COLOR);
continueBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
continueBtn.addActionListener(e -> {
transition.dispose();
SwingUtilities.invokeLater(onComplete);
});
JPanel messageCard = 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);
GradientPaint gp = new GradientPaint(0, 0, CARD_BG, 0, getHeight(), new Color(30, 50, 75));
g2d.setPaint(gp);
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 28, 28);
g2d.setColor(new Color(ACCENT_COLOR.getRed(), ACCENT_COLOR.getGreen(), ACCENT_COLOR.getBlue(), 140));
g2d.setStroke(new BasicStroke(2.5f));
g2d.drawRoundRect(1, 1, getWidth() - 3, getHeight() - 3, 28, 28);
g2d.dispose();
}
};
messageCard.setOpaque(false);
messageCard.setLayout(new BoxLayout(messageCard, BoxLayout.Y_AXIS));
messageCard.setBorder(BorderFactory.createEmptyBorder(50, 60, 50, 60));
messageCard.setPreferredSize(new Dimension(520, 320));
messageCard.setMaximumSize(new Dimension(520, 320));
msgLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
subLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
continueBtn.setAlignmentX(Component.CENTER_ALIGNMENT);
messageCard.add(Box.createVerticalGlue());
messageCard.add(msgLabel);
messageCard.add(Box.createRigidArea(new Dimension(0, 15)));
messageCard.add(subLabel);
messageCard.add(Box.createRigidArea(new Dimension(0, 40)));
messageCard.add(continueBtn);
messageCard.add(Box.createVerticalGlue());
overlayPanel.add(messageCard);
transition.add(overlayPanel);
transition.setVisible(true);
}
public void endTurn() {
showTransitionScreen("Zug beendet",
game.getActivePlayerName() + " ist am Zug", () -> {
gamePanel.refreshBoards();
});
}
public void showGameOver(String winner) {
resultPanel.showResult(winner);
cardLayout.show(mainPanel, "result");
}
public void newGame() {
game = null;
player1Name = null;
player2Name = null;
setupPanel.resetState();
gamePanel.resetState();
resultPanel.stopCelebration();
showWelcome();
}
public void showSettingsDialog() {
JDialog dialog = createSettingsDialog();
dialog.setVisible(true);
}
public void leaveCurrentGame() {
newGame();
}
public void closeGame() {
dispose();
System.exit(0);
}
public GAME getGame() {
return game;
}
public List<ShipType> getFleetDefinition() {
return fleetDefinition;
}
public int getBoardSize() {
return boardSize;
}
private JDialog createSettingsDialog() {
final int dialogWidth = 560;
final int dialogHeight = 470;
final int dialogCornerRadius = 28;
JDialog dialog = new JDialog(this, true);
dialog.setUndecorated(true);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dialog.setResizable(false);
dialog.setSize(dialogWidth, dialogHeight);
dialog.setBackground(new Color(0, 0, 0, 0));
dialog.setShape(new RoundRectangle2D.Double(0, 0, dialogWidth, dialogHeight, dialogCornerRadius, dialogCornerRadius));
dialog.setLocationRelativeTo(this);
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);
GradientPaint gp = new GradientPaint(0, 0, OCEAN_DARK, getWidth(), getHeight(), OCEAN_MEDIUM);
g2d.setPaint(gp);
g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 28, 28);
g2d.setColor(new Color(ACCENT_COLOR.getRed(), ACCENT_COLOR.getGreen(), ACCENT_COLOR.getBlue(), 130));
g2d.setStroke(new BasicStroke(2f));
g2d.drawRoundRect(1, 1, getWidth() - 3, getHeight() - 3, 28, 28);
g2d.dispose();
}
};
panel.setOpaque(false);
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
panel.setBorder(BorderFactory.createEmptyBorder(22, 34, 26, 34));
JPanel titleBar = new JPanel(new BorderLayout());
titleBar.setOpaque(false);
JLabel titleLabel = new JLabel("Einstellungen");
titleLabel.setFont(TITLE_FONT.deriveFont(30f));
titleLabel.setForeground(TEXT_PRIMARY);
JButton closeButton = createDialogCloseButton();
closeButton.addActionListener(e -> dialog.dispose());
titleBar.add(titleLabel, BorderLayout.WEST);
titleBar.add(closeButton, BorderLayout.EAST);
ModernButton leaveButton = new ModernButton("Spiel verlassen", WARNING_COLOR);
leaveButton.setAlignmentX(Component.CENTER_ALIGNMENT);
leaveButton.setPreferredSize(new Dimension(320, 52));
leaveButton.addActionListener(e -> {
dialog.dispose();
confirmLeaveCurrentGame();
});
ModernButton closeGameButton = new ModernButton("Spiel schließen", HIT_COLOR);
closeGameButton.setAlignmentX(Component.CENTER_ALIGNMENT);
closeGameButton.setPreferredSize(new Dimension(320, 52));
closeGameButton.addActionListener(e -> {
dialog.dispose();
confirmCloseGame();
});
ModernButton resumeButton = new ModernButton("Zurück", ACCENT_COLOR);
resumeButton.setAlignmentX(Component.CENTER_ALIGNMENT);
resumeButton.setPreferredSize(new Dimension(220, 50));
resumeButton.addActionListener(e -> dialog.dispose());
panel.add(titleBar);
panel.add(Box.createRigidArea(new Dimension(0, 36)));
panel.add(leaveButton);
panel.add(Box.createRigidArea(new Dimension(0, 28)));
panel.add(closeGameButton);
panel.add(Box.createRigidArea(new Dimension(0, 28)));
panel.add(resumeButton);
panel.add(Box.createVerticalGlue());
dialog.setContentPane(panel);
dialog.getRootPane().setOpaque(false);
dialog.getRootPane().setDefaultButton(resumeButton);
dialog.getRootPane().registerKeyboardAction(
e -> dialog.dispose(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_IN_FOCUSED_WINDOW
);
return dialog;
}
private JButton createDialogCloseButton() {
JButton button = new JButton("x");
button.setFont(HEADER_FONT);
button.setForeground(TEXT_PRIMARY);
button.setFocusPainted(false);
button.setBorder(BorderFactory.createEmptyBorder(6, 10, 6, 10));
button.setContentAreaFilled(false);
button.setOpaque(false);
button.setCursor(new Cursor(Cursor.HAND_CURSOR));
return button;
}
private void confirmLeaveCurrentGame() {
int selection = JOptionPane.showOptionDialog(
this,
"Das aktuelle Spiel wird beendet und du kehrst zum Startbildschirm zurück.",
"Spiel verlassen?",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
new Object[]{"Spiel verlassen", "Abbrechen"},
"Abbrechen"
);
if (selection == JOptionPane.YES_OPTION) {
leaveCurrentGame();
}
}
private void confirmCloseGame() {
int selection = JOptionPane.showOptionDialog(
this,
"Die Anwendung wird sofort geschlossen.",
"Spiel schließen?",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE,
null,
new Object[]{"Spiel schließen", "Abbrechen"},
"Abbrechen"
);
if (selection == JOptionPane.YES_OPTION) {
closeGame();
}
}
public static void main(String[] args) {
// Set system look and feel improvements
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
// Use default
}
setGlobalUIFont(BattleshipGUI.LABEL_FONT);
SwingUtilities.invokeLater(() -> {
BattleshipGUI gui = new BattleshipGUI();
gui.setVisible(true);
});
}
private static void setGlobalUIFont(Font font) {
FontUIResource resource = new FontUIResource(font);
Enumeration<Object> keys = UIManager.getDefaults().keys();
while (keys.hasMoreElements()) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, resource);
}
}
}
}