-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrawingPage.java
More file actions
executable file
·432 lines (329 loc) · 11.3 KB
/
Copy pathDrawingPage.java
File metadata and controls
executable file
·432 lines (329 loc) · 11.3 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
import java.awt.Graphics;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.text.*;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class DrawingPage{
static int time;
public static String[] q;
// Get random word from the database by sending a request to the server
public static String[] wordArray () {
String[] word= Client.getNeededInfor("GET_WORD", HomePage.categoryName);
return word;
}
static Timer t;
public static void main(String[] args){
JFrame frame = new JFrame("Draw Something");
Container content = frame.getContentPane();
frame.getContentPane().setLayout(null);
JLabel lblTopic = new JLabel("Topic :");
lblTopic.setBounds(10, 79, 188, 41);
frame.getContentPane().add(lblTopic);
PadDraw drawPad = new PadDraw();
drawPad.setBounds(12, 133, 577, 443);
content.add(drawPad);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 482, 68);
panel.setPreferredSize(new Dimension(100, 68));
panel.setMinimumSize(new Dimension(100, 68));
panel.setMaximumSize(new Dimension(100, 68));
JButton twoX = new JButton ("2");
twoX.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.clear();
}
});
JButton yellowButton = new JButton("YELLOW");
yellowButton.setForeground(new Color(204, 204, 0));
yellowButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.yellow();
}
});
JButton greenButton = new JButton("GREEN");
greenButton.setForeground(new Color(0, 204, 0));
greenButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.green();
}
});
JButton redButton = new JButton("RED");
redButton.setForeground(new Color(255, 0, 0));
redButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.red();
}
});
JButton blueButton = new JButton("BLUE");
blueButton.setForeground(Color.BLUE);
blueButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.blue();
}
});
JButton blackButton = new JButton("BLACK");
blackButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.black();
}
});
JButton sendButton = new JButton("SEND");
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
//We can use the JPEG if we want to save user drawings or
//whatever in the future without referring to DB.
//drawPad.saveComponentAsJPEG(drawPad,"C:/test.jpg");
//Send byte to server for storage.
String myBytes = drawPad.saveComponentAsByte(drawPad);
//Image is sent to server.
drawPad.sendImage(myBytes);
//drawPad.ByteToImage(myBytes, "C:/translated.jpg");
t.stop();
HomePage homePage = new HomePage();
homePage.Home();
// Close previous screen
frame.dispose();
}
});
greenButton.setPreferredSize(new Dimension(80, 20));
redButton.setPreferredSize(new Dimension(80, 20));
yellowButton.setPreferredSize(new Dimension(80, 20));
blueButton.setPreferredSize(new Dimension(80, 20));
blackButton.setPreferredSize(new Dimension(80,20));
sendButton.setPreferredSize(new Dimension(100,20));
panel.add(blackButton);
panel.add(blueButton);
panel.add(redButton);
panel.add(greenButton);
panel.add(yellowButton);
JLabel Countdown = new JLabel("Ready?");
panel.add(Countdown);
panel.add(sendButton);
drawPad.addMouseListener(new MouseAdapter() {
int sec =time;
@Override
public void mousePressed(MouseEvent e) {
t =new Timer(1000,new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
Countdown.setForeground(Color.RED);
if(sec<1)
{
JOptionPane.showMessageDialog(null, "Times up");
t.stop();
Countdown.setText("Times Over");
content.remove(drawPad);
String myBytes = drawPad.saveComponentAsByte(drawPad);
//Image is sent to server.
drawPad.sendImage(myBytes);
JOptionPane.showMessageDialog(null, "Add your image successfully");
HomePage home = new HomePage();
home.Home();
frame.dispose();
}else
{
sec--;
Countdown.setText(" "+sec);
}
}
});
drawPad.removeMouseListener(this);
t.start();
}
});
content.add(panel);
JRadioButton rdbtnPx = new JRadioButton("3 px");
panel.add(rdbtnPx);
JRadioButton rdbtnPx_1 = new JRadioButton("5 px");
panel.add(rdbtnPx_1);
JRadioButton rdbtnPx_2 = new JRadioButton("12 px");
panel.add(rdbtnPx_2);
ButtonGroup bg = new ButtonGroup();
bg.add(rdbtnPx);
bg.add(rdbtnPx_1);
bg.add(rdbtnPx_2);
rdbtnPx.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.small();
}
});
rdbtnPx_1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.medium();
}
});
rdbtnPx_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.big();
}
});
JButton clearButton = new JButton("Clear");
clearButton.setBackground(new Color(255, 255, 255));
clearButton.setFont(UIManager.getFont("TextArea.font"));
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
drawPad.clear();
}
});
panel.add(clearButton);
frame.setSize(677, 636);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Get a random word
q = wordArray();
// Display the random generated word
lblTopic.setText("Topic: "+ q[1]);
}
public DrawingPage(String diffLevel) {
getClass() ;
getTime(diffLevel);
}
public void getTime(String diffLevel)
{
switch(diffLevel)
{
case "Easy" :
time =180;
break;
case "Hard" :
time =60;
break;
case "Intermediate" :
time =120;
break;
}
}
}
class PadDraw extends JComponent{
private Image image;
private Graphics2D graphics2D;
private int currentX , currentY , oldX , oldY ;
public PadDraw(){
setDoubleBuffered(false);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
oldX = e.getX();
oldY = e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
currentX = e.getX();
currentY = e.getY();
if(graphics2D != null)
graphics2D.drawLine(oldX, oldY, currentX, currentY);
repaint();
oldX = currentX;
oldY = currentY;
}
});
}
public void paintComponent(Graphics g){
if(image == null){
image = createImage(getSize().width, getSize().height);
graphics2D = (Graphics2D)image.getGraphics();
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
clear();
}
g.drawImage(image, 5, 5, null);
}
public void clear(){
graphics2D.setPaint(Color.white);
graphics2D.fillRect(0, 0, getSize().width, getSize().height);
graphics2D.setPaint(Color.black);
repaint();
}
public void red(){
graphics2D.setPaint(Color.red);
repaint();
}
public void black(){
graphics2D.setPaint(Color.black);
repaint();
}
public void yellow(){
graphics2D.setPaint(Color.yellow);
repaint();
}
public void blue(){
graphics2D.setPaint(Color.blue);
repaint();
}
public void green(){
graphics2D.setPaint(Color.green);
repaint();
}
public void small(){
graphics2D.setStroke(new BasicStroke(1));;
}
public void medium(){
graphics2D.setStroke(new BasicStroke(5));;
}
public void big(){
graphics2D.setStroke(new BasicStroke(12));;
}
public void sendImage(String g) {
//connect to server, convert to binary and send.
String[] userID= Client.getNeededInfor("USER_INFO");
Client.imagesend(userID[1], DrawingPage.q[2], HomePage.difficultLevel,g);
}
public void saveComponentAsJPEG(Component myComponent, String filename) {
Dimension size = myComponent.getSize();
BufferedImage myImage =
new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
try {
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(myImage);
out.close();
} catch (Exception e) {
System.out.println(e);
}
}
public String saveComponentAsByte(Component myComponent) {
Dimension size = myComponent.getSize();
BufferedImage myImage =
new BufferedImage(size.width, size.height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = myImage.createGraphics();
myComponent.paint(g2);
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(myImage, "jpeg", outputStream);
String encodedImage = Base64.encode(outputStream.toByteArray());
System.out.println(encodedImage);
return encodedImage;
} catch (Exception e) {
System.out.println(e);
}
return null; //something failed.
}
public static void ByteToImage(String bytes, String filename) {
String data = "data:image/jpeg;base64,"+bytes;
String base64Image = data.split(",")[1];
byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(base64Image);
try {
BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageBytes));
OutputStream out = new FileOutputStream(filename);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(img);
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}