-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.java
More file actions
81 lines (68 loc) · 2.59 KB
/
Application.java
File metadata and controls
81 lines (68 loc) · 2.59 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
package com.lmtuan.app.simplequestion;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Application extends JFrame {
private static final long serialVersionUID = -7579064373078078135L;
public static final String TITLE = "IQ Test";
public static long TIME_LIMIT = 3 * 60000; //3 mins
private long startTime;
private int quitAttempts = 0;
public Application() {
super(TITLE);
startTime = System.currentTimeMillis();
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
quitConfirm();
}
});
GUI mainPane = new GUI(e1 -> yesConfirm(), e2 -> noConfirm());
this.add(mainPane);
this.pack();
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public void quitConfirm() {
++quitAttempts;
if (quitAttempts > 2 || (System.currentTimeMillis() - startTime > TIME_LIMIT)) {
JOptionPane.showMessageDialog(this, "Sometimes the only way to win is not to play.",
"You figured it out", JOptionPane.INFORMATION_MESSAGE);
this.dispose();
} else if (quitAttempts == 2) {
JOptionPane.showMessageDialog(this, "Hint: it is possible if you are fast enough",
"Just a bit more", JOptionPane.WARNING_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Try harder!",
"Quitting so soon?", JOptionPane.WARNING_MESSAGE);
}
}
public void yesConfirm() {
JOptionPane.showMessageDialog(this, "Glad you realized it.", "Well..", JOptionPane.QUESTION_MESSAGE);
this.dispose();
}
public void noConfirm() {
long sec = (System.currentTimeMillis() - startTime) / 100;
int ans = JOptionPane.showConfirmDialog(this, "You just wasted " + sec + " seconds.\nReplay?",
"You sure?", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (ans == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(this, "You are stupid.", "That's it", JOptionPane.WARNING_MESSAGE);
}
this.dispose();
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel (UIManager.getSystemLookAndFeelClassName ());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException
| UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater( () -> new Application() );
}
}