-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
217 lines (180 loc) · 7.61 KB
/
Main.java
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
//Cindy & Stella
//ICS3U Culminating Project
//June 12, 2023
//imports necessary libraries
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.Border;
import Board.GameBoard; //imports the boardgame
public class Main {
//creates necessary variables for the game to function
private static JPanel contentPanel;
private static CardLayout cardLayout;
public static int numOfPlayers;
private static String [] playerUsernames;
public static void startScreen() {
JFrame welcomeFrame = new JFrame("Culminating Game Project");
welcomeFrame.setLayout(new BorderLayout());
welcomeFrame.setSize(500, 360);
welcomeFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create the content panel with CardLayout
contentPanel = new JPanel();
cardLayout = new CardLayout();
contentPanel.setLayout(cardLayout);
// First screen: title and start button
JPanel titlePanel = createTitlePanel();
contentPanel.add(titlePanel, "title");
// Second screen: Set up Players
JPanel playerPanel = createPlayerPanel();
contentPanel.add(playerPanel, "player");
// Third screen: Set up Usernames
JPanel usernamePanel = new JPanel(); // Create an empty panel for now
contentPanel.add(usernamePanel, "username");
// Fourth screen: Instructions
JPanel instructionsPanel = createInstructionsPanel();
contentPanel.add(instructionsPanel, "instructions");
welcomeFrame.add(contentPanel, BorderLayout.CENTER);
welcomeFrame.setVisible(true);
}
//Title Screen
private static JPanel createTitlePanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("<html><div style='text-align:center;'>"
+ "<span style='font-size:20px;'>Dice Dash</span><br>"
+ "<span style='font-size:18px;'>Cindy & Stella</span><br>"
+ "<span style='font-size:16px;'>June 12, 2023</span></div></html>");
// border preferences
Border border = BorderFactory.createLineBorder(Color.BLACK);
label.setBorder(border);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
label.setPreferredSize(new Dimension(400, 200));
panel.add(label, BorderLayout.CENTER);
// Start button: title screen
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
cardLayout.show(contentPanel, "player");// Transition to the "player" screen
}
});
panel.add(startButton, BorderLayout.SOUTH);
return panel;
}
// Player Screen
public static JPanel createPlayerPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel titleLabel = new JLabel("<html><div style='text-align:center;'>"
+ "<span style='font-size:20px;'>Choose Number of Players (2-4)</span></div></html>");
titleLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
titleLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(titleLabel, BorderLayout.NORTH);
JPanel inputPanel = new JPanel(new FlowLayout());
JLabel inputLabel = new JLabel("Number of Players: ");
JTextField inputField = new JTextField(5);
inputPanel.add(inputLabel);
inputPanel.add(inputField);
panel.add(inputPanel, BorderLayout.CENTER);
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String inputText = inputField.getText().trim();
try {
int inputNum = Integer.parseInt(inputText);
if (inputNum >= 2 && inputNum <= 4) {
numOfPlayers = inputNum; // Store the valid number of players
createUsernamePanel(); // Call the username panel creation method
cardLayout.show(contentPanel, "username");
} else { JOptionPane.showMessageDialog(panel,
"Invalid number of players. Please enter a number between 2 and 4.",
"Invalid Input", JOptionPane.ERROR_MESSAGE);
}
} catch (NumberFormatException ex) {
JOptionPane.showMessageDialog(panel,
"Invalid input format. Please enter a number between 2 and 4.",
"Invalid Input", JOptionPane.ERROR_MESSAGE);
}
}
});
panel.add(nextButton, BorderLayout.SOUTH);
return panel;
}
// Username Screen
public static void createUsernamePanel() {
JPanel usernamePanel = new JPanel();
usernamePanel.setLayout(new BorderLayout());
JLabel titleLabel = new JLabel("<html><div style='text-align:center;'>"
+ "<span style='font-size:20px;'>Usernames</span></div></html>");
titleLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
titleLabel.setHorizontalAlignment(JLabel.CENTER);
usernamePanel.add(titleLabel, BorderLayout.NORTH);
JPanel userPanelsContainer = new JPanel(); // Create a new container for user panels
userPanelsContainer.setLayout(new BoxLayout(userPanelsContainer, BoxLayout.Y_AXIS)); // Use vertical BoxLayout
JTextField[] userFields = new JTextField[numOfPlayers]; // Array to store the text fields for usernames
for (int i = 0; i < numOfPlayers; i++) {
JPanel userPanel = new JPanel(new FlowLayout());
JLabel userLabel = new JLabel("Enter Username for Player " + (i + 1));
JTextField userField = new JTextField(7);
userPanel.add(userLabel);
userPanel.add(userField);
userPanelsContainer.add(userPanel); // Add each user panel to the container
userFields[i] = userField; // Store the text field in the array
}
usernamePanel.add(userPanelsContainer, BorderLayout.CENTER); // Add the container to the main panel
// Next button
JButton nextButton = new JButton("Next");
nextButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playerUsernames = new String[numOfPlayers]; // Array to store the usernames
for (int i = 0; i < numOfPlayers; i++) {
playerUsernames[i] = userFields[i].getText().trim();
}
// Transition to the instructions screen
cardLayout.show(contentPanel, "instructions");
}
});
usernamePanel.add(nextButton, BorderLayout.SOUTH);
// Replace the empty "username" panel with the actual panel
contentPanel.remove(2);
contentPanel.add(usernamePanel, "username");
contentPanel.revalidate();
}
//Instructions Screen
private static JPanel createInstructionsPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel label = new JLabel("<html><div style='text-align:center;'>"
+ "<span style='font-size:20px;'>Dice Dash Instructions</span><br>"
+ "<span style='font-size:13px;'>\n In this game each player rolls the dice and moves forward on the board the number that they roll.\n\n HOWEVER IF you land on a special square you get to play a mini game for FUN!\n\nThe player to reach the end of the board first is the winner!\n\nGood luck and have fun!!</span></div></html>");
// border preferences
Border border = BorderFactory.createLineBorder(Color.BLACK);
label.setBorder(border);
label.setHorizontalAlignment(JLabel.CENTER);
label.setVerticalAlignment(JLabel.CENTER);
label.setPreferredSize(new Dimension(400, 200));
panel.add(label, BorderLayout.CENTER);
// Next Button
JButton gameboardButton = new JButton("Next");
gameboardButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
GameBoard game = new GameBoard(playerUsernames);
}
});
panel.add(gameboardButton, BorderLayout.SOUTH);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
startScreen();
}
});
}
}