-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewGame.java
190 lines (155 loc) · 6.89 KB
/
NewGame.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
package computerscienceia;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.border.Border;
public class NewGame extends javax.swing.JPanel{
Main parent;
Font mainFont = new Font("Comic Sans", 0, 20);
Border buttonBorder = BorderFactory.createLineBorder(Color.gray, 3);
JTextField timeInput;
boolean checked = false;
public NewGame(Main Parent){
this.parent = Parent;
createNewGame();
}
public void createNewGame(){
// GUI Setup
this.setLayout(null);
JLabel screenLabel = new JLabel();
screenLabel.setFont(new Font("Comic Sans", 1, 40));
screenLabel.setBounds(0, 0, 900, 120);
screenLabel.setHorizontalAlignment(JLabel.CENTER);
screenLabel.setText("Game Setup");
screenLabel.setForeground(new Color(100, 100, 100));
this.add(screenLabel);
JButton returnButton = new JButton();
returnButton.setBounds(760, 20, 100, 30);
returnButton.setFont(mainFont);
returnButton.setBorder(buttonBorder);
returnButton.setText("Back");
returnButton.setForeground(new Color(100, 100, 100));
returnButton.setHorizontalAlignment(JLabel.CENTER);
returnButton.addActionListener((ActionEvent e) -> {
parent.createGUI("main");
});
this.add(returnButton);
// UserInput fields
JTextField[] playerInput = new JTextField[2];
JLabel[] playerLabels = new JLabel[2];
String[] labelTexts = new String[]{"White Player", "Black Player"};
for (int i = 0; i < 2; i++){
playerInput[i] = new JTextField();
playerLabels[i] = new JLabel();
playerInput[i].setBorder(buttonBorder);
playerLabels[i].setText(labelTexts[i]);
playerInput[i].setForeground(new Color(100, 100, 100));
if (i == 0){
playerLabels[i].setForeground(new Color(255, 255, 255));
} else if (i == 1){
playerLabels[i].setForeground(new Color(0, 0, 0));
}
playerInput[i].setBounds(400, 140 + (100*i), 300, 50);
playerLabels[i].setBounds(200, 140 + (100*i), 200, 50);
playerInput[i].setFont(mainFont);
playerLabels[i].setFont(new Font("Comic Sans", 1, 20));
playerInput[i].setHorizontalAlignment(JLabel.CENTER);
playerLabels[i].setHorizontalAlignment(JLabel.CENTER);
this.add(playerInput[i]);
this.add(playerLabels[i]);
}
timeInput = new JTextField();
timeInput.setForeground(new Color(100, 100, 100));
timeInput.setFont(mainFont);
timeInput.setBorder(buttonBorder);
timeInput.setHorizontalAlignment(JLabel.CENTER);
timeInput.setBounds(400, 340, 150, 50);
timeInput.setEnabled(false);
this.add(timeInput);
JLabel timerE = new JLabel();
timerE.setText("Timer");
timerE.setFont(new Font("Comic Sans", 1, 20));
timerE.setForeground(new Color(100, 100, 100));
timerE.setBounds(200, 340, 200, 50);
timerE.setHorizontalAlignment(JLabel.CENTER);
this.add(timerE);
JLabel finish = new JLabel();
finish.setText("minutes");
finish.setFont(mainFont);
finish.setForeground(new Color(100, 100, 100));
finish.setHorizontalAlignment(JLabel.CENTER);
finish.setBounds(550, 340, 100, 50);
this.add(finish);
JButton timeCheckButton = new JButton();
timeCheckButton.setBounds(350, 352, 28, 25);
timeCheckButton.setContentAreaFilled(false);
timeCheckButton.setFocusPainted(false);
timeCheckButton.setBorderPainted(false);
try {
timeCheckButton.setIcon(new ImageIcon(ImageIO.read(Main.class.getResourceAsStream("/resources/UnChecked.png"))));
} catch (IOException ex) {
ex.printStackTrace();
}
timeCheckButton.addActionListener((ActionEvent e) -> {
if (checked == false){
checked = true;
try {
timeCheckButton.setIcon(new ImageIcon(ImageIO.read(Main.class.getResourceAsStream("/resources/Checked.png"))));
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (checked == true){
checked = false;
try {
timeCheckButton.setIcon(new ImageIcon(ImageIO.read(Main.class.getResourceAsStream("/resources/UnChecked.png"))));
} catch (IOException ex) {
ex.printStackTrace();
}
timeInput.setText("");
}
timeInput.setEnabled(checked);
parent.repaint();
});
this.add(timeCheckButton);
JButton confirmation = new JButton();
confirmation.setBounds(300, 450, 300, 80);
confirmation.setForeground(new Color(100, 100, 100));
confirmation.setFont(mainFont);
confirmation.setHorizontalAlignment(JLabel.CENTER);
confirmation.setBorder(buttonBorder);
confirmation.addActionListener((ActionEvent e) -> {
final String playerW = playerInput[0].getText();
final String playerB = playerInput[1].getText();
// Set game parameters and start a new game
if ((!playerW.equals("") && !playerB.equals("")) && !playerW.equals(playerB)){
try{
int rTime = 0;
if (checked == false){
rTime = -1;
} else {
rTime = (int)(Double.parseDouble(timeInput.getText())*60);
}
if (rTime >= 120 || !checked){
parent.gameParameters = new ChessGame(playerW, playerB, rTime);
parent.createGUI("board");
} else {
javax.swing.JOptionPane.showMessageDialog(parent, "Time input too small");
}
} catch (Exception m){
javax.swing.JOptionPane.showMessageDialog(parent, "Invalid timer Input");
}
} else {
javax.swing.JOptionPane.showMessageDialog(parent, "Player names may be empty or the same");
}
});
confirmation.setText("Begin Game");
this.add(confirmation);
}
}