-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquestions.java
170 lines (134 loc) · 6.02 KB
/
questions.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
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.* ;
import javax.swing.border.LineBorder;
import javax.swing.border.SoftBevelBorder;
class questions extends JPanel {
JLabel Q ,
hintLabel;
JButton option1 ,
option2,
option3,
option4 ;
String correct_answer,
hint ;
static boolean next = false ;
static int score = 0 ;
static JLabel timer = new JLabel ("00 : 00 : 000") ;
static counter count = new counter ();
questions (quiz2 obj , JFrame window) {
Q = new JLabel (obj.question);
hintLabel = new JLabel("Hint");
option1 = new JButton (obj.op1) ;
option2 = new JButton (obj.op2) ;
option3 = new JButton (obj.op3) ;
option4 = new JButton (obj.op4) ;
correct_answer = obj.correct_answer ;
hint = obj.hint ;
JPanel pan = new JPanel () ;
pan.setLayout(null);
pan.setBorder(BorderFactory.createLineBorder(Color.gray));
pan.setBackground(Color.DARK_GRAY);
window.setContentPane(pan);
setLayout(null);
setBackground(Color.getHSBColor(154, 254, 25));
setBounds(90,170,600,200);
setBorder(BorderFactory.createLineBorder(Color.black));
pan.add(this);
add(Q); add(option1); add(option2); add(option3); add(option4);
Q.setBounds(110,8,400,50);
Q.setBorder(new LineBorder(Color.blue, 2, true));
Q.setHorizontalAlignment(JLabel.CENTER);
option1.setBounds(90,70,200,40); option1.setBackground(new Color(255,255,255)) ;
option2.setBounds(90,140,200,40); option2.setBackground(new Color(255,255,255)) ;
option3.setBounds(330,70,200,40); option3.setBackground(new Color(255,255,255)) ;
option4.setBounds(330,140,200,40); option4.setBackground(new Color(255,255,255)) ;
option1.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
option2.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
option3.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
option4.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
timer.setBounds(250,400,300,50);
timer.setFont(new Font("Verdana", Font.BOLD, 40));
timer.setHorizontalAlignment(JLabel.CENTER);
timer.setBorder(BorderFactory.createLineBorder(Color.white));
timer.setForeground(Color.white);
pan.add(timer);
JButton hintBTN = new JButton(new ImageIcon(getClass().getResource("hint.png")));
JButton half = new JButton(new ImageIcon(getClass().getResource("50 50.png")));
pan.add(half); pan.add(hintBTN); pan.add(hintLabel);
hintBTN.setBounds(200,50,80,80);
hintBTN.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
half.setBounds(500,50,80,80);
half.setBorder(new SoftBevelBorder(SoftBevelBorder.RAISED));
hintLabel.setBounds(290,50,200,80);
hintLabel.setHorizontalAlignment(JLabel.CENTER);
hintLabel.setForeground(Color.white);
hintLabel.setBorder(new LineBorder(Color.white, 2, true));
hintBTN.addActionListener((ActionEvent e) -> {
hintLabel.setText(hint);
});
half.addActionListener((ActionEvent e) -> {
if (!option1.getText().equals(correct_answer)) {
option1.setEnabled(false);
option1.setBackground(Color.red);
}else if (!option2.getText().equals(correct_answer)) {
option2.setEnabled(false);
option2.setBackground(Color.red);
}
if (!option3.getText().equals(correct_answer)) {
option3.setEnabled(false);
option3.setBackground(Color.red);
}else if (!option4.getText().equals(correct_answer)) {
option4.setEnabled(false);
option4.setBackground(Color.red);
}
});
window.setVisible(true);
}
void getAnswer (int time) throws InterruptedException {
option1.addActionListener((ActionEvent e) -> {
if (option1.getText().equals(correct_answer)) score++ ;
next = true ;
});
option2.addActionListener((ActionEvent e) -> {
if (option2.getText().equals(correct_answer)) score++ ;
next = true ;
});
option3.addActionListener((ActionEvent e) -> {
if (option3.getText().equals(correct_answer)) score++ ;
next = true ;
});
option4.addActionListener((ActionEvent e) -> {
if (option4.getText().equals(correct_answer)) score++ ;
next = true ;
});
while (next == false ) {
timer.setText(String.format("%02d", count.M)+" : "+String.format("%02d", count.S)+" : "+String.format("%03d", count.Ms));
count.Ms++ ;
Thread.sleep(1);
if (count.Ms==999){
count.S++ ;
count.Ms=0 ;
}
if (count.S==59){
count.M++ ;
count.S=0;
}
if ((count.S + count.M*60) > time-3 ) {
timer.setForeground(Color.red);
if ((count.S + count.M*60)==time) {
return ;
}
}
}
next = false ;
}
int getScore() {return score ;}
counter getTime () {return count ;}
void Reset () {
count.M=0 ;
count.Ms=0 ;
count.S=0;
score = 0 ;
}
}