-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathStartMenu.java
More file actions
154 lines (124 loc) · 5.38 KB
/
StartMenu.java
File metadata and controls
154 lines (124 loc) · 5.38 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
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class StartMenu implements Runnable {
public void run() {
final JFrame startWindow = new JFrame("Chess");
// Set window properties
startWindow.setLocation(300,100);
startWindow.setResizable(false);
startWindow.setSize(260, 240);
Box components = Box.createVerticalBox();
startWindow.add(components);
// Game title
final JPanel titlePanel = new JPanel();
components.add(titlePanel);
final JLabel titleLabel = new JLabel("Chess");
titlePanel.add(titleLabel);
// Black player selections
final JPanel blackPanel = new JPanel();
components.add(blackPanel, BorderLayout.EAST);
final JLabel blackPiece = new JLabel();
try {
Image blackImg = ImageIO.read(getClass().getResource("bp.png"));
blackPiece.setIcon(new ImageIcon(blackImg));
blackPanel.add(blackPiece);
} catch (Exception e) {
System.out.println("Required game file bp.png missing");
}
final JTextField blackInput = new JTextField("Black", 10);
blackPanel.add(blackInput);
// White player selections
final JPanel whitePanel = new JPanel();
components.add(whitePanel);
final JLabel whitePiece = new JLabel();
try {
Image whiteImg = ImageIO.read(getClass().getResource("wp.png"));
whitePiece.setIcon(new ImageIcon(whiteImg));
whitePanel.add(whitePiece);
startWindow.setIconImage(whiteImg);
} catch (Exception e) {
System.out.println("Required game file wp.png missing");
}
final JTextField whiteInput = new JTextField("White", 10);
whitePanel.add(whiteInput);
// Timer settings
final String[] minSecInts = new String[60];
for (int i = 0; i < 60; i++) {
if (i < 10) {
minSecInts[i] = "0" + Integer.toString(i);
} else {
minSecInts[i] = Integer.toString(i);
}
}
final JComboBox<String> seconds = new JComboBox<String>(minSecInts);
final JComboBox<String> minutes = new JComboBox<String>(minSecInts);
final JComboBox<String> hours =
new JComboBox<String>(new String[] {"0","1","2","3"});
Box timerSettings = Box.createHorizontalBox();
hours.setMaximumSize(hours.getPreferredSize());
minutes.setMaximumSize(minutes.getPreferredSize());
seconds.setMaximumSize(minutes.getPreferredSize());
timerSettings.add(hours);
timerSettings.add(Box.createHorizontalStrut(10));
timerSettings.add(seconds);
timerSettings.add(Box.createHorizontalStrut(10));
timerSettings.add(minutes);
timerSettings.add(Box.createVerticalGlue());
components.add(timerSettings);
// Buttons
Box buttons = Box.createHorizontalBox();
final JButton quit = new JButton("Quit");
quit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startWindow.dispose();
}
});
final JButton instr = new JButton("Instructions");
instr.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(startWindow,
"To begin a new game, input player names\n" +
"next to the pieces. Set the clocks and\n" +
"click \"Start\". Setting the timer to all\n" +
"zeroes begins a new untimed game.",
"How to play",
JOptionPane.PLAIN_MESSAGE);
}
});
final JButton start = new JButton("Start");
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String bn = blackInput.getText();
String wn = whiteInput.getText();
int hh = Integer.parseInt((String) hours.getSelectedItem());
int mm = Integer.parseInt((String) minutes.getSelectedItem());
int ss = Integer.parseInt((String) seconds.getSelectedItem());
new GameWindow(bn, wn, hh, mm, ss);
startWindow.dispose();
}
});
buttons.add(start);
buttons.add(Box.createHorizontalStrut(10));
buttons.add(instr);
buttons.add(Box.createHorizontalStrut(10));
buttons.add(quit);
components.add(buttons);
Component space = Box.createGlue();
components.add(space);
startWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
startWindow.setVisible(true);
}
}