-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessingPage.java
More file actions
executable file
·298 lines (226 loc) · 7.34 KB
/
Copy pathGuessingPage.java
File metadata and controls
executable file
·298 lines (226 loc) · 7.34 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
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.imageio.ImageIO;
import javax.print.DocFlavor.URL;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;
public class GuessingPage {
private JFrame frame;
private JLabel lblUserName;
private JLabel lblLevel;
private JLabel lblXP;
private JTextField textFieldGuessing;
private static JLabel lbl;
public static String[] imageInfo;
String[] timePeriod;
public int time;
String[] image;
static String diffLevel;
/**
* Launch the application.
*/
public static void main(String[] args ) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuessingPage window = new GuessingPage();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuessingPage() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
Timer t;
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 1000, 800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textFieldGuessing = new JTextField();
textFieldGuessing.setBounds(87, 700, 319, 51);
frame.getContentPane().add(textFieldGuessing);
textFieldGuessing.setColumns(10);
JButton btnGuessing = new JButton("Answer");
btnGuessing.setBounds(460, 700, 147, 51);
frame.getContentPane().add(btnGuessing);
JLabel lblNameHere = new JLabel("Guessing game ");
lblNameHere.setFont(new Font("Algerian", Font.PLAIN, 30));
lblNameHere.setBounds(286, 13, 300, 66);
frame.getContentPane().add(lblNameHere);
lblUserName = new JLabel("User Name here");
lblUserName.setBounds(12, 76, 115, 16);
frame.getContentPane().add(lblUserName);
lblLevel = new JLabel("Their current level");
lblLevel.setBounds(12, 105, 153, 16);
frame.getContentPane().add(lblLevel);
lblXP = new JLabel("Their current Xp");
lblXP.setBounds(12, 134, 99, 16);
frame.getContentPane().add(lblXP);
DisplayUsersInformation();
JLabel Countdown = new JLabel("Ready?");
Countdown.setBounds(22, 163, 111, 33);
frame.getContentPane().add(Countdown);
// Use to display the image
lbl=new JLabel();
lbl.setBounds(110, 200, 700, 500);
frame.getContentPane().add(lbl);
System.out.println("this is a test");
imageInfo = getImageAndAnsw();
if(imageInfo[0].equals("Fail")) {
lbl.setText("There are no available images! Please try again later");
lbl.setFont(new Font("Serif", Font.PLAIN, 20));
Countdown.setText("");
textFieldGuessing.setEditable(false);
btnGuessing.setEnabled(false);
JButton btn = new JButton("Finish");
btn.setBounds(460, 500, 147, 51);
frame.getContentPane().add(btn);
btn.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
HomePage home = new HomePage();
home.Home();
frame.dispose();
}
});
}
else {
convertToImage(imageInfo[2]);
displayImage();
frame.getContentPane().addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
frame.getContentPane().removeMouseListener(this);
t =new Timer();
t.scheduleAtFixedRate(new TimerTask() {
int i = time;
@Override
public void run() {
Countdown.setForeground(Color.RED);
Countdown.setText(""+(i--));
if(i<0)
{
t.cancel();
JOptionPane.showMessageDialog(null, "Times up");
Countdown.setText("Times Over");
textFieldGuessing.disable();
frame.dispose();
FinishScreen fs = new FinishScreen();
fs.main(null);
}
}
}, 0, 1000);
}
});
getTime(HomePage.difficultLevel);
}
// Event handler when user click guess
btnGuessing.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
// Get user's answer
String userAnswer = textFieldGuessing.getText();
// Get correction answer and drawing id
// Get user ID
String[] userID= Client.getNeededInfor("USER_INFO");
// Compare correct answer
if( userAnswer.toLowerCase().equals(imageInfo[1].toLowerCase())) {
JOptionPane.showMessageDialog(null, "That answer is correct !");
//DifficultyLevel,DrawingID,UserID,SucceedTimes,TotalTime
Client.insertGuess(HomePage.difficultLevel,imageInfo[3],userID[1],"1","1");
Client.updatePoint(userID[1], HomePage.difficultLevel,imageInfo[4],imageInfo[3]);
// Ensure that user wont receive the same image that they gave the correct answer
Client.insertCorrectGuess(userID[1],imageInfo[3]);
t.cancel();
navigateScreen();
}
else {
JOptionPane.showMessageDialog(null, "Wrong answer!/nPlease Try Again");
Client.insertGuess(HomePage.difficultLevel,imageInfo[3],userID[1],"0","1");
// t.cancel();
//navigateScreen();
}
}
});
}
public void navigateScreen() {
FinishScreen fn = new FinishScreen();
fn.main(new String[] {"sthg"});
frame.dispose();
}
public static void displayImage() {
// Get default path
String path = System.getProperty("user.dir");
BufferedImage img = null;
try {
// Read converted image
// from application directory
img = ImageIO.read(new File(path + "//translatedImage.jpeg"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ImageIcon icon=new ImageIcon(img);
lbl.setIcon(icon);
}
public static void convertToImage(String imageCode) {
// Convert image byte code back to an image
PadDraw.ByteToImage(imageCode, "translatedImage.jpeg");
}
// Get image and answer array
public static String[] getImageAndAnsw() {
// [1]- correct answer
// [2]- image encoded
// [3]- drawing ID
String[] userID= Client.getNeededInfor("USER_INFO");
String[] image = Client.getNeededInfor("GET_IMAGE", userID[1],HomePage.categoryName);
return image;
}
private void DisplayUsersInformation() {
// Variables that will hold user's username, level and xp
String[] userInfor = Client.getNeededInfor("USER_INFO");
// Get information from the database
lblUserName.setText("Name: " +userInfor[2]);
lblLevel.setText("Current level: "+userInfor[3]);
lblXP.setText("Current Xp: "+userInfor[4]);
}
public void getTime(String diffLevel)
{
switch(diffLevel)
{
case "Easy" :
time =180;
break;
case "Hard" :
time =60;
break;
case "Intermediate" :
time =120;
break;
}
}
}