-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuizApplication.java
More file actions
476 lines (408 loc) · 17.9 KB
/
QuizApplication.java
File metadata and controls
476 lines (408 loc) · 17.9 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/**
* This is simple Java Swing GUI based Quiz Application program.
* This Quiz game has three stages 1-> 10 Question(Easy) 2-> 20 Question(Medium) 3-> 30 Question(Hard)
* If you select the 10-Q(Easy) Button then the current frame is disable and two new frame is created
* One of the frame is Question frame and other frame is help frame.
* Each Question frame is divided by 3 JPanel. First Panel take Question. 2nd panel take first two button and
* third panel take 2nd two button.
* The help frame is divided by 2 panel. First panel show a message and the 2nd panel take take 3 button with option
* Change Question, Pass Question and 50-50 Chance option
* You can change the question clicking by change question button, pass question by clicking pass question button and to
* take a 50-50 chance you need to click 50-50 button
*
* Then after all if you click on the question option button then question frame and help frame is currently dispose
* and two new frame is created instantly. One frame show your answer is correct or not and another frame is showing
* the your current score.
*
* Continue..................................later added some more documentation.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.awt.event.WindowEvent;
public class QuizApplication {
public static void main(String[] args) {
new Quiz(); // Anonymous object
}
}
class Quiz extends JFrame {
private ImageIcon ii;
private JPanel buttonPanel, labelPanel,hbuttonPanel, hlabelPanel, qbPanel1, qbPanel2, imagePanel;
private JButton button, button1, button2, button3, button4, hbutton1, hbutton2, hbutton3;
private String[] all_lines;
private int limit = 0, counter = 1, idx = 0, score = 0;
private JFrame qFrame, hFrame, mFrame, sFrame;
private String s, path;
private Path smile, currentPath, thanks, think, sad, question;
public Quiz() {
super("Choose Difficulty");
currentPath = Paths.get(System.getProperty("user.dir"));
smile = Paths.get(currentPath.toString(), "smile.gif");
thanks = Paths.get(currentPath.toString(), "thanks.gif");
think = Paths.get(currentPath.toString(), "think.gif");
question = Paths.get(currentPath.toString(), "Question.txt");
sad = Paths.get(currentPath.toString(), "sad.gif");
setLayout(new GridLayout(2, 1));
setBounds(448, 282, 500, 150);
labelPanel = new JPanel();
labelPanel.add(new JLabel("How many questions would you like to answer?"));
buttonPanel = new JPanel(new FlowLayout());
ii = new ImageIcon(new ImageIcon(think.toString()).getImage().getScaledInstance(25, 20, Image.SCALE_DEFAULT));
// call createButton function
button1 = CreateButton("10(Easy)", ii, "Click this to answer 10 question");
button2 = CreateButton("20(Medium)", ii, "Click this to answer 20 question");
button3 = CreateButton("30(Hard)", ii, "Click this to answer 30 question");
buttonPanel.add(button1); buttonPanel.add(button2); buttonPanel.add(button3);
add(labelPanel);
add(buttonPanel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
// call getData(null)
getData();
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button1) {
dispose();
limit = 10;
QuestionAndHelp();
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button2) {
dispose();
limit = 20;
QuestionAndHelp();
}
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button3) {
dispose();
limit = 30;
QuestionAndHelp();
}
}
});
}
public JButton CreateButton(String label, ImageIcon ii, String toolTipText) {
button = new JButton(label, ii);
button.setToolTipText(toolTipText);
button.setBorderPainted(false);
ButtonHover(button);
return button;
}
public void ButtonHover(JButton button) {
button.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseEntered(java.awt.event.MouseEvent evt) {
button.setBackground(Color.GREEN);
}
public void mouseExited(java.awt.event.MouseEvent evt) {
button.setBackground(UIManager.getColor("control"));
}
});
}
public void QuestionAndHelp() {
//Question frame portion
s = "Question " + String.valueOf(counter);
qFrame = new JFrame(s);
qFrame.setBounds(157,235, 500, 200);
qFrame.setLayout(new GridLayout(3,1));
ii = new ImageIcon(new ImageIcon(think.toString()).getImage().getScaledInstance(25, 20, Image.SCALE_DEFAULT));
labelPanel = new JPanel();
labelPanel.add(new JLabel(all_lines[idx++]));
button1 = CreateButton(all_lines[idx++], ii, "Select Option 1");
button2 = CreateButton(all_lines[idx++], ii, "Select Option 2");
button3 = CreateButton(all_lines[idx++], ii, "Select Option 3");
button4 = CreateButton(all_lines[idx++], ii, "Select Option 4");
qbPanel1 = new JPanel();
qbPanel2 = new JPanel();
qbPanel1.add(button1); qbPanel1.add(button2);
qbPanel2.add(button3); qbPanel2.add(button4);
qFrame.add(labelPanel);
qFrame.add(qbPanel1); qFrame.add(qbPanel2);
// Help frame portion
s = "Help-lines";
hFrame = new JFrame(s);
hFrame.setBounds(740, 251, 500, 150);
hFrame.setLayout(new GridLayout(2, 1));
ii = new ImageIcon(new ImageIcon(think.toString()).getImage().getScaledInstance(25, 20, Image.SCALE_DEFAULT));
hlabelPanel = new JPanel();
hlabelPanel.add(new JLabel("You can use the following Help-Lines one time each" , SwingConstants.CENTER));
hbutton1 = CreateButton("Change Question", ii, "Click this to change question");
hbutton2 = CreateButton("Pass Question", ii, "Click this to pass question");
hbutton3 = CreateButton("50-50", ii, "Click this to disable to two button");
hbuttonPanel = new JPanel(new FlowLayout());
hbuttonPanel.add(hbutton1); hbuttonPanel.add(hbutton2); hbuttonPanel.add(hbutton3);
hFrame.add(hlabelPanel);
hFrame.add(hbuttonPanel);
button1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button1) {
counter++;
qFrame.dispose();
hFrame.dispose();
if(button1.getText().equals(all_lines[idx])) {
idx++;
MessageAndAnswer(true);
} else {
idx++;
MessageAndAnswer(false);
}
}
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button2) {
counter++;
qFrame.dispose();
hFrame.dispose();
if(button2.getText().equals(all_lines[idx])) {
idx++;
MessageAndAnswer(true);
} else {
idx++;
MessageAndAnswer(false);
}
}
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button3) {
counter++;
qFrame.dispose();
hFrame.dispose();
if(button3.getText().equals(all_lines[idx])) {
idx++;
MessageAndAnswer(true);
} else {
idx++;
MessageAndAnswer(false);
}
}
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button4) {
counter++;
qFrame.dispose();
hFrame.dispose();
if(button4.getText().equals(all_lines[idx])) {
idx++;
MessageAndAnswer(true);
} else {
idx++;
MessageAndAnswer(false);
}
}
}
});
hbutton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == hbutton1) {
hFrame.dispose();
qFrame.dispose();
idx++;
hbutton1.setEnabled(false);
hbutton2.setEnabled(false);
hbutton3.setEnabled(false);
QuestionAndHelp();
}
}
});
hbutton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == hbutton2) {
counter++;
hFrame.dispose();
qFrame.dispose();
hbutton2.setEnabled(false);
hbutton1.setEnabled(false);
hbutton2.setEnabled(false);
idx++;
if ( counter <= limit) {
QuestionAndHelp();
} else {
FinalResult();
}
}
}
});
hbutton3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == hbutton3) {
int inner = idx - 5;
String answer = all_lines[idx];
hbutton3.setEnabled(false);
hbutton1.setEnabled(false);
hbutton2.setEnabled(false);
int check = -1;
for(int i = inner; i < idx; i++) {
if(answer.equals(all_lines[i])) {
check = i;
break;
}
}
if((check - inner) % 4 == 1) {
button3.setEnabled(false);
button2.setEnabled(false);
} else if ((check - inner) % 4 == 2) {
button1.setEnabled(false);
button4.setEnabled(false);
} else if ((check - inner ) % 4 == 3) {
button1.setEnabled(false);
button2.setEnabled(false);
} else {
button2.setEnabled(false);
button3.setEnabled(false);
}
}
}
});
qFrame.pack();
hFrame.setVisible(true);
qFrame.setVisible(true);
}
public void MessageAndAnswer(boolean flag) {
mFrame = new JFrame("Message");
mFrame.setBounds(462, 212, 325,325);
mFrame.setLayout(new GridLayout(3,1));
sFrame = new JFrame("Score");
sFrame.setBounds(853, 277, 200,200);
sFrame.setLayout(new GridLayout(2, 1));
labelPanel = new JPanel();
if (flag) {
score = score + 10;
JPanel messageTrueContentPanel = new JPanel();
JPanel messageTrueImagePanel = new JPanel();
JPanel messageTrueButtonPanel = new JPanel(new FlowLayout());
ii = new ImageIcon(new ImageIcon(smile.toString()).getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
mFrame.add(messageTrueContentPanel.add(new JLabel("Correct Answer. You get 10 Marks.", SwingConstants.CENTER)));
mFrame.add(messageTrueImagePanel.add(new JLabel(ii)));
button = new JButton("Ok");
messageTrueButtonPanel.add(button);
mFrame.add(messageTrueButtonPanel);
sFrame.add(new JLabel("Your Score is shown below\n", SwingConstants.CENTER));
sFrame.add(new JLabel(String.valueOf(score), SwingConstants.CENTER));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (actionEvent.getSource() == button) {
if(counter <= limit) {
sFrame.dispose();
mFrame.dispose();
QuestionAndHelp();
} else {
mFrame.dispose();
sFrame.dispose();
FinalResult();
}
}
}
});
mFrame.setVisible(true);
sFrame.setVisible(true);
} else {
score = score - 5;
JPanel messageFalseContentPanel = new JPanel();
JPanel messageFalseImagePanel = new JPanel();
JPanel messageFalseButtonPanel = new JPanel(new FlowLayout());
ii = new ImageIcon(new ImageIcon(sad.toString()).getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
mFrame.add(messageFalseContentPanel.add(new JLabel("Wrong answer. You get -5 Marks.", SwingConstants.CENTER)));
mFrame.add(messageFalseImagePanel.add(new JLabel(ii)));
button = new JButton("Ok");
messageFalseButtonPanel.add(button);
mFrame.add(messageFalseButtonPanel);
sFrame.add(new JLabel("Your Score is shown below\n", SwingConstants.CENTER));
sFrame.add(new JLabel(String.valueOf(score), SwingConstants.CENTER));
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == button) {
if(counter <= limit) {
sFrame.dispose();
mFrame.dispose();
QuestionAndHelp();
} else {
mFrame.dispose();
sFrame.dispose();
FinalResult();
}
}
}
});
mFrame.setVisible(true);
sFrame.setVisible(true);
}
}
public void FinalResult() {
JFrame finalMessageFrame = new JFrame("Message");
JFrame finalScoreFrame = new JFrame("Score");
finalMessageFrame.setBounds(493, 199, 325, 325);
finalScoreFrame.setBounds(851, 256, 300, 200);
finalMessageFrame.setLayout(new GridLayout(3,1));
JPanel finalMessageFrameContentPanel = new JPanel();
JPanel finalMessageFrameImagePanel = new JPanel();
JPanel finalMessageFrameButtonPanel = new JPanel(new FlowLayout());
JLabel finalMessageFrameLabel = new JLabel("Thank you for playing the game.", SwingConstants.CENTER);
finalMessageFrameContentPanel.add(finalMessageFrameLabel);
finalMessageFrame.add(finalMessageFrameContentPanel);
ii = new ImageIcon(new ImageIcon(thanks.toString()).getImage().getScaledInstance(150, 150, Image.SCALE_DEFAULT));
finalMessageFrameImagePanel.add(new JLabel(ii));
finalMessageFrame.add(finalMessageFrameImagePanel);
JButton finalMessageFrameButton = new JButton("Ok");
finalMessageFrameButtonPanel.add(finalMessageFrameButton);
finalMessageFrame.add(finalMessageFrameButtonPanel);
finalMessageFrameButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if(actionEvent.getSource() == finalMessageFrameButton) {
System.exit(0);
}
}
});
finalScoreFrame.setLayout(new GridLayout(2, 1));
finalScoreFrame.add(new JLabel("Your score is shown below.", SwingConstants.CENTER));
finalScoreFrame.add(new JLabel(String.valueOf(score), SwingConstants.CENTER));
finalScoreFrame.setVisible(true);
finalMessageFrame.setVisible(true);
}
public void getData() {
all_lines = new String[300];
try {
File f = new File(question.toString());
BufferedReader b = new BufferedReader(new FileReader(f));
String readLine = "";
int i = 0;
while ((readLine = b.readLine()) != null) {
all_lines[i] = readLine;
i += 1;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}