Repository navigation
Expand file tree
/
Copy pathUI.java
More file actions
372 lines (344 loc) · 10.4 KB
/
Copy pathUI.java
File metadata and controls
372 lines (344 loc) · 10.4 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
package Hangman;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.awt.event.ActionEvent;
import javax.swing.SwingConstants;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.Label;
import javax.swing.JComboBox;
public class UI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
*
*/
private JPanel contentPane;
private JButton bStart;
private JButton bQuit;
private JLabel lTitle;
private JButton btnNewButton;
private JPanel pStart;
private JPanel pRules;
private JButton btnBack;
private JPanel pGame;
private Hangman h;
private JButton[] buttons;
private JButton bBack;
private JPanel bpanel;
private JLabel hangingImage;
private Label progress;
private JLabel word;
private JButton playagain;
private String filename;
private JComboBox comboBox;
private String name;
private int numCorrect;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UI frame = new UI();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
* @throws IOException
*/
public UI() throws IOException {
int count = 0;
this.name = "default";
if (count == 0) { this.name = getName(); count++;}
this.numCorrect = 0;
leaderboard(name, numCorrect);
initContainers();
actions();
}
public String getName() {
Scanner in = new Scanner(System.in);
System.out.print("What is your name? ");
return in.nextLine();
}
/**
* Adds to leaderboard the information obtained from the game.
*/
public void leaderboard(String player, int solved) throws IOException {
HangmanLeaderboard hl = new HangmanLeaderboard(player, solved);
String fileName = "HangmanLeaderboard.txt";
//read existing file
ArrayList<String> lead = hl.readFile(fileName);
hl.addWordsSolved(lead);
hl.writeFile(lead);
hl.printArrayList(lead);
}
public void makeButtons() {
// TODO Auto-generated method stub
int j = 65;
for (int i = 0; i < buttons.length; i++) {
char c = (char) j;
String letter = "" + c;
buttons[i] = new JButton(letter);
bpanel.add(buttons[i]);
j++;
}
bpanel.setVisible(true);
for (int i = 0; i < buttons.length; i++) {
final int index = i;
buttons[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (h.checkLetter(buttons[index].getText()))
buttons[index].setForeground(Color.GREEN);
else
buttons[index].setVisible(false);
progress.setText(h.getProgress());
hangingImage.setIcon(h.getImage());
if (h.getLives() == 0) {
bpanel.setVisible(false);
word.setText("The word was: " + h.getWord());
playagain.setVisible(true);
} else if (h.wordEquals()) {
bpanel.setVisible(false);
playagain.setVisible(true);
try {
addLeaderBoard();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
h.playSound("img/win.wav");
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
}
}
public void initContainers() throws IOException {
filename = "words.txt";
buttons = new JButton[26];
setTitle("Hangman");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 900, 600);
contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(null);
start();
}
/*
* Creates the start JPanel and adds components.
*/
public void start() {
pStart = new JPanel();
pStart.setBackground(new Color(25, 25, 112));
pStart.setBounds(0, 0, 900, 600);
contentPane.add(pStart);
pStart.setLayout(null);
bStart = new JButton("Start");
bStart.setFont(new Font("Copperplate", Font.BOLD, 18));
bStart.setBackground(new Color(255, 255, 255));
bStart.setBounds(300, 147, 300, 100);
pStart.add(bStart);
title();
}
public void title() {
lTitle = new JLabel("Hangman\n");
lTitle.setForeground(new Color(255, 255, 255));
lTitle.setBounds(175, 6, 565, 139);
pStart.add(lTitle);
lTitle.setHorizontalAlignment(SwingConstants.CENTER);
lTitle.setFont(new Font("Wawati SC", Font.BOLD | Font.ITALIC, 99));
quit();
}
public void quit() {
bQuit = new JButton("Quit");
bQuit.setFont(new Font("Copperplate", Font.BOLD, 16));
bQuit.setBounds(300, 403, 300, 100);
pStart.add(bQuit);
rules();
}
public void rules() {
btnNewButton = new JButton("Rules");
btnNewButton.setFont(new Font("Copperplate", Font.BOLD, 16));
btnNewButton.setBounds(300, 280, 300, 100);
pStart.add(btnNewButton);
categories();
}
public void categories() {
String[] items = { "words.txt", "countries.txt", "computer.txt", "food.txt" , "school.txt" };
comboBox = new JComboBox(items);
comboBox.setBounds(365, 246, 176, 27);
pStart.add(comboBox);
game();
}
public void game() {
pGame = new JPanel();
pGame.setBackground(new Color(0, 128, 128));
pGame.setEnabled(false);
pGame.setBounds(0, 0, 900, 600);
contentPane.add(pGame);
game1();
}
public void game1() {
pGame.setLayout(null);
playagain = new JButton("Play again?");
playagain.setBounds(373, 450, 117, 29);
pGame.add(playagain);
playagain.setVisible(false);
word = new JLabel("");
word.setFont(new Font("Lucida Grande", Font.PLAIN, 25));
word.setBounds(373, 400, 414, 61);
pGame.add(word);
back();
}
public void back() {
bpanel = new JPanel();
bpanel.setBackground(new Color(0, 128, 128));
bpanel.setBounds(100, 416, 703, 270);
pGame.add(bpanel);
bBack = new JButton("Back");
bBack.setFont(new Font("Copperplate", Font.BOLD, 16));
bBack.setBounds(710, 37, 173, 61);
pGame.add(bBack);
pGame.setVisible(false);
hangman();
}
public void hangman() {
hangingImage = new JLabel();
hangingImage.setBounds(150, 37, 687, 315);
pGame.add(hangingImage);
progress();
}
public void progress () {
progress = new Label();
progress.setAlignment(Label.CENTER);
progress.setFont(new Font("Comic Sans MS", Font.PLAIN, 40));
progress.setBounds(10, 332, 873, 76);
pGame.add(progress);
rulesP();
}
/*
* Creates JPanel rules and creates buttons and text box.
*/
public void rulesP() {
pRules = new JPanel();
pRules.setBackground(new Color(123, 104, 238));
pRules.setBounds(0, 0, 900, 600);
contentPane.add(pRules);
pRules.setLayout(null);
backB();
}
public void backB() {
btnBack = new JButton("Back");
btnBack.setFont(new Font("Copperplate", Font.BOLD, 14));
btnBack.setBounds(349, 420, 190, 91);
pRules.add(btnBack);
scrollPane();
}
public void scrollPane () {
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(6, 6, 888, 370);
pRules.add(scrollPane);
JTextPane txtpnTheWordTo = new JTextPane();
txtpnTheWordTo.setFont(new Font("Comic Sans MS", Font.BOLD, 16));
txtpnTheWordTo.setEditable(false);
txtpnTheWordTo.setText(
"The word to guess is represented by a row of dashes, representing each letter of the word. If the guessing player suggests a letter which occurs in the word, the other player writes it in all its correct positions. If the suggested letter or number does not occur in the word, the other player draws one element of a hanged man stick figure as a tally mark.\nThe player guessing the word may, at any time, attempt to guess the whole word. If the word is correct, the game is over and the guesser wins. Otherwise, the other player may choose to penalize the guesser by adding an element to the diagram. On the other hand, if the other player makes enough incorrect guesses to allow his opponent to complete the diagram, the game is also over, this time with the guesser losing. However, the guesser can also win by guessing all the letters or numbers that appears in the word, thereby completing the word, before the diagram is completed.\nIn our version of Hangman, you want to accumulate the most points as possible. Make sure to watch the Eclipse Counsole for in-game messages!\n\nGood Luck!");
scrollPane.setViewportView(txtpnTheWordTo);
pRules.setVisible(false);
/*
* Creates the JPanel pGame and adds components.
*/
/*
* Creates JPanel bpanel and calls makeButtons for the button array.
*/
makeButtons();
}
public void updateLabel(String c) {
filename = c;
}
public void actions() {
comboBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox<String> combo = (JComboBox<String>) e.getSource();
String choice = (String) combo.getSelectedItem();
updateLabel(choice);
}
});
bQuit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
bStart.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pGame.setVisible(true);
pStart.setVisible(false);
startGame();
}
});
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pStart.setVisible(false);
pRules.setVisible(true);
}
});
btnBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pStart.setVisible(true);
pRules.setVisible(false);
}
});
bBack.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
pStart.setVisible(true);
pRules.setVisible(false);
pGame.setVisible(false);
}
});
playagain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startGame();
}
});
}
public void startGame() {
h = new Hangman(filename);
hangingImage.setIcon(h.getImage());
progress.setText(h.getProgress());
bpanel.setVisible(true);
word.setText("");
for (JButton b : buttons) {
b.setForeground(Color.BLACK);
b.setVisible(true);
}
playagain.setVisible(false);
}
public void addLeaderBoard() throws IOException{
numCorrect++;
leaderboard(name, numCorrect);
}
}