Repository navigation
Expand file tree
/
Copy pathPentrisMainMenu.java
More file actions
110 lines (88 loc) · 4.05 KB
/
Copy pathPentrisMainMenu.java
File metadata and controls
110 lines (88 loc) · 4.05 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
package main;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PentrisMainMenu extends JFrame {
// Convenience constants
private static final Font TITLE_FONT = new Font("Monospaced", Font.BOLD, 36);
private static final Font BUTTON_FONT = new Font("Monospaced", Font.PLAIN, 18);
private static final Dimension BTN_SZ = new Dimension(160, 40);
public PentrisMainMenu() {
super("Pentris – Main Menu");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//setPreferredSize(new Dimension(1280, 720));
setResizable(false);
/* ---------- MAIN CONTENT PANEL ---------- */
JPanel content = new JPanel();
content.setBorder(BorderFactory.createEmptyBorder(20, 40, 20, 40));
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
/* ---------- TITLE ---------- */
JLabel title = new JLabel("Pentris", SwingConstants.CENTER);
title.setAlignmentX(Component.CENTER_ALIGNMENT);
title.setFont(TITLE_FONT);
content.add(title);
content.add(Box.createRigidArea(new Dimension(0, 30))); // spacing
/* ---------- DIFFICULTY RADIO-BUTTONS ---------- */
JPanel diffPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 0));
diffPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
JRadioButton easy = new JRadioButton("Easy", true);
JRadioButton normal = new JRadioButton("Normal");
JRadioButton hard = new JRadioButton("Hard");
ButtonGroup group = new ButtonGroup();
group.add(easy);
group.add(normal);
group.add(hard);
diffPanel.add(easy);
diffPanel.add(normal);
diffPanel.add(hard);
content.add(diffPanel);
content.add(Box.createRigidArea(new Dimension(0, 40))); // spacing
/* ---------- ACTION BUTTONS ---------- */
JButton newGameBtn = makeMenuButton("New Game");
JButton loadGameBtn = makeMenuButton("See saved games");
JButton exitBtn = makeMenuButton("Exit");
// Attach simple action handlers (replace with your own logic)
newGameBtn.addActionListener(e -> {
String difficulty = getSelectedDifficulty(group, easy, normal, hard);
Main.launchGame(difficulty);
});
loadGameBtn.addActionListener(e -> {
MainImageLooker.launch();
});
exitBtn.addActionListener(e -> System.exit(0));
// Put the three buttons into a vertically stacked panel
Box buttonBox = Box.createVerticalBox();
buttonBox.setAlignmentX(Component.CENTER_ALIGNMENT);
buttonBox.add(newGameBtn);
buttonBox.add(Box.createRigidArea(new Dimension(0, 10)));
buttonBox.add(loadGameBtn);
buttonBox.add(Box.createRigidArea(new Dimension(0, 10)));
buttonBox.add(exitBtn);
content.add(buttonBox);
/* ---------- FINALISE ---------- */
getContentPane().add(content);
pack(); // size to preferred sizes
setLocationRelativeTo(null); // center on screen
setVisible(true);
}
/** Utility to create uniformly styled buttons. */
private static JButton makeMenuButton(String text) {
JButton btn = new JButton(text);
btn.setPreferredSize(BTN_SZ);
btn.setFont(BUTTON_FONT);
return btn;
}
/** Helper to read which difficulty is chosen. */
private static String getSelectedDifficulty(ButtonGroup g,
JRadioButton easy,
JRadioButton normal,
JRadioButton hard) {
if (easy.isSelected()) return "EASY";
if (normal.isSelected()) return "NORMAL";
if (hard.isSelected()) return "HARD";
return "UNKNOWN";
}
public static void main(String[] args) {
SwingUtilities.invokeLater(PentrisMainMenu::new);
}
}