-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS_GUI.java
More file actions
862 lines (746 loc) · 28.7 KB
/
Copy pathDS_GUI.java
File metadata and controls
862 lines (746 loc) · 28.7 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.swing.*;
import org.json.simple.JSONObject;
class DS_GUI extends JFrame implements ItemListener , KeyListener,WindowListener{
private JButton ovalBt;
private JButton circleBt;
private JButton rectangleBt;
private JButton lineBt;
private JButton eraserBt;
private JButton penBt;
private JButton textBt;
public Gshape shape;
private JComboBox colorBt;
private Color c = Color.black;// should check if it's ok
private JMenuBar JMenuBar;// define the menu bar
private JMenu JMenuOpen;
private JMenu JMenuNew;
private JMenu JMenuSave;
private JMenu JMenuSaveAs;
private JMenu JMenuClose;// not sure if it's useful?
private JFrame jfile;
private String filePath;
private BufferedImage myImage = null;
private static int count = 0;// count
private int action = 0;
private Manager manager;
private DataOutputStream os = null;
private int startX = 0;
private int startY = 0;
private int endX;
private int endY;
private String text="";
static Color[] colors = {Color.black, Color.blue, Color.RED,Color.yellow,Color.green,Color.orange,Color.cyan, Color.darkGray,Color.gray,Color.lightGray,Color.magenta,Color.pink,Color.white,new Color(227, 207, 87),new Color(88, 87, 86),new Color(255, 127, 80),new Color(116, 0, 0)};
static String[] strings = {"Black", "Blue", "Red","Yellow","Green","Orange","Cyan","Dark Gray","Gray","Light Gray","Magenta", "Pink","White","Banana Yellow","Ivory Black","Coral Red","Black Red"};
public DS_GUI(Manager manager) {
this.manager = manager;
this.os = manager.getOS();
shape = new Gshape(this.os);
// implement the button
ovalBt = new JButton("Oval");
circleBt = new JButton("Circle");
rectangleBt = new JButton("Rectangle");
lineBt = new JButton("Line");
eraserBt = new JButton("eraser");
penBt = new JButton("pen");
colorBt = new JComboBox(strings);
textBt = new JButton("text");
textBt.addKeyListener(this);
// implement the menu
JMenuBar = new JMenuBar();
JMenuOpen = new JMenu("Open");
JMenuNew = new JMenu("New");
JMenuSave = new JMenu("Save");
JMenuSaveAs = new JMenu("SaveAs");
JMenuClose = new JMenu("Close");
// add the menu in menu bar
JMenuBar.add(JMenuOpen);
JMenuBar.add(JMenuNew);
JMenuBar.add(JMenuSave);
JMenuBar.add(JMenuSaveAs);
// JMenuBar.add(JMenuClose);
// create the panel
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new GridLayout(4, 1));
btnPanel.add(lineBt);
btnPanel.add(rectangleBt);
btnPanel.add(circleBt);
btnPanel.add(ovalBt);
btnPanel.add(eraserBt);
btnPanel.add(penBt);
btnPanel.add(colorBt);
btnPanel.add(textBt);
ComboBoxRenderer renderer = new ComboBoxRenderer(colorBt);
renderer.setColors(colors);
renderer.setStrings(strings);
colorBt.setRenderer(renderer);
// color add
// colorBt.addItem("black");
// colorBt.addItem("blue");
// colorBt.addItem("red");
// colorBt.addItem("yellow");
// colorBt.addItem("green");
// colorBt.addItem("orange");
// colorBt.addItem("cyan");
// colorBt.addItem("darkGray");
// colorBt.addItem("gray");
// colorBt.addItem("lightGray");
// colorBt.addItem("magenta");
// colorBt.addItem("pink");
// colorBt.addItem("white");
// colorBt.addItem("bananaYellow");
// colorBt.addItem("ivoryRlack");
// colorBt.addItem("coralRed");
// colorBt.addItem("blackRed");
colorBt.addItemListener(this);
// create the container
Container content = this.getContentPane();// get the pane
content.setLayout(new BorderLayout());
content.add(btnPanel, BorderLayout.WEST);// add the btn
content.add(shape, BorderLayout.CENTER);// add the wb
// content.add(JMenuBar,BorderLayout.NORTH);//add the menu
setJMenuBar(JMenuBar);// this is also ok for menu bar
// this part is to add action for the button
lineBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("line");
action = 1;
}
});
rectangleBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("rectangle");
action = 1;
}
});
circleBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("circle");
action = 1;
}
});
ovalBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("oval");
action = 1;
}
});
eraserBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("eraser");
action = 1;
}
});
penBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("pen");
action = 1;
}
});
textBt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
shape.setObject("text");
action = 1;
}
});
// this part is for the textInput
// this part is to add action for the menu
JMenuOpen.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
shape.setObject("");
/*
* int width = shape.getWidth(); int height = shape.getHeight(); BufferedImage
* bufImage = (BufferedImage) shape.createImage(width, height); Graphics2D gc =
* bufImage.createGraphics(); gc.setColor(Color.white); gc.fillRect(0, 0, width,
* height);
*/
openDialog();
Graphics g = shape.getGraphics();
Graphics2D g2 = (Graphics2D) g;
BufferedImage bufImage;
try {
if(!filePath.equals("")) {
bufImage = ImageIO.read(new File(filePath));
shape.setBufImage(bufImage);
g2.drawImage(bufImage, null, 0, 0);
}
int[][] matrix = getMatrixofImage();
String row = String.valueOf(matrix.length);
String col = String.valueOf(matrix[1].length);
try {
String matrixString = convertToString(matrix, matrix.length,
matrix[0].length);
int bufferSize = 60000;
int i = 0;
int sum = 0;
int count = 0;
while (i < matrixString.length()) {
int endIdx = java.lang.Math.min(matrixString.length(), i + bufferSize);
String partMatrixString = matrixString.substring(i, endIdx);
JSONObject drawUpdate = new JSONObject();
drawUpdate.put("command_name", "openFile");
drawUpdate.put("row", row);
drawUpdate.put("col", col);
drawUpdate.put("which", "0"); // manager id
drawUpdate.put("content", partMatrixString);
if (count == 0) {
drawUpdate.put("identifier", "start");
} else {
drawUpdate.put("identifier", "notStart");
}
sum += partMatrixString.length();
i += bufferSize;
count++;
if (sum == matrixString.length()) {
drawUpdate.put("finish", "True");
} else {
drawUpdate.put("finish", "False");
}
os.writeUTF(drawUpdate.toString());
os.flush();
}
assert sum == matrixString.length();
}catch(IIOException e2) {
//System.out.println("Cannot read file.");
JOptionPane.showMessageDialog(null, "Cannot read file");
} catch (Exception e1) {
// TODO Auto-generated catch block
//System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
} catch (Exception e1) {
// TODO Auto-generated catch block
//System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
}
});
JMenuNew.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
shape.setObject("");
JSONObject command = new JSONObject();
command.put("command_name", "newPicture");
try {
os.writeUTF(command.toString());
os.flush();
} catch (Exception e1) {
// TODO Auto-generated catch block
//System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
/*
* JFrame newgui=new DS_GUI(); newgui.setBounds(100, 100, 600, 437);
* newgui.setTitle("Shared Whiteboard"); newgui.setVisible(true);
* newgui.setBackground(Color.white);
*/
// shape.getGraphics();
// newgui.setVisible(true);
int width = shape.getWidth();
int height = shape.getHeight();
BufferedImage bufImage = (BufferedImage) shape.createImage(width, height);
Graphics2D gc = bufImage.createGraphics();
gc.setColor(Color.white);
gc.fillRect(0, 0, width, height);
Graphics g = shape.getGraphics();
Graphics2D g2 = (Graphics2D) g;
shape.setBufImage(bufImage);
g2.drawImage(bufImage, null, 0, 0);
}
});
JMenuSave.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
// String path=System.getProperty("user.dir"); //get current directory
try {
/*
* int height=shape.getBufImage().getHeight(); int
* width=shape.getBufImage().getWidth(); int x=shape.getX(); int
* y=shape.getY(); myImage = new Robot().createScreenCapture(new
* Rectangle(x,y,width,height));
*/
myImage = shape.getBufImage();
ImageIO.write(myImage, "jpg", new File("./image" + count + ".jpg"));
count++;
} catch (Exception e1) {
// TODO Auto-generated catch block
// System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
}
});
JMenuSaveAs.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
saveDialog();
myImage = shape.getBufImage();
try {if(!filePath.equals("")) {
ImageIO.write(myImage, "jpg", new File(filePath + ".jpg"));
}
} catch (Exception e1) {
// TODO Auto-generated catch block
// System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
}
});
JMenuClose.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
System.exit(0);
}
});
this.pack();// adjust the size of btn auto
}
private void openDialog() {
FileDialog fd = new FileDialog(jfile, "Open", FileDialog.LOAD);
fd.setVisible(true);
if(fd.getDirectory()!=null) {
/*
* while(fd.getFile()==null) { continue; }
*/
filePath = fd.getDirectory() + fd.getFile();
}
else {
filePath="";
}
}
private void saveDialog() {
FileDialog fd = new FileDialog(jfile, "SaveAs", FileDialog.SAVE);
fd.setVisible(true);
/*
* while(fd.getFile()==null) { continue; }
*/
if (fd.getDirectory() != null) {
filePath = fd.getDirectory() + fd.getFile();
}else {
filePath = "";
}
}
public int[][] getMatrixofImage() {
BufferedImage bimg;
bimg = shape.getBufImage();
int[][] data = new int[bimg.getWidth()][bimg.getHeight()];
for (int i = 0; i < bimg.getWidth(); i++) {
for (int j = 0; j < bimg.getHeight(); j++) {
data[i][j] = bimg.getRGB(i, j);
}
}
return data;
}
public void updateImage(int[][] rgbValue2) {
// int[][] rgbValue1 = getMatrixofImage();
// row
int width = rgbValue2.length;
// column
int height = rgbValue2[0].length;
// composite a new array
// int[][] compositeRgbValue = new int[width][height];
// for (int x = 0; x < width; x++) {
// for (int y = 0; y < height; y++) {
// // RGB is binary need to implement bitwise
// // red = (pixel >> 16) & 0xFF;
// if ((((rgbValue1[x][y] >> 16) & 0xFF) == 0xFE)
// || (((rgbValue2[x][y] >> 16) & 0xFF) == 0xFE)) {
// compositeRgbValue[x][y] = 0xffffffff;
// } else {
// compositeRgbValue[x][y] = rgbValue1[x][y] & rgbValue2[x][y];
// }
// }
// }
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bufferedImage.setRGB(x, y, rgbValue2[x][y]);
}
}
Graphics g = shape.getGraphics();
Graphics2D g2 = (Graphics2D) g;
shape.setBufImage(bufferedImage);
g2.drawImage(bufferedImage, null, 0, 0);
}
public void setAction(int action) {
this.action = action;
}
public int getAction() {
return action;
}
/*
* (non-Javadoc)
*
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
*/
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == colorBt) {// ÑÕÉ«
String s = colorBt.getSelectedItem().toString();
if (s.equals("Black")) {
c = Color.black;
shape.setColor(c);
}
if (s.equals("Blue")) {
c = Color.blue;
shape.setColor(c);
}
if (s.equals("Red")) {
c = Color.red;
shape.setColor(c);
}
if (s.equals("Yellow")) {
c = Color.yellow;
shape.setColor(c);
}
if (s.equals("Green")) {
c = Color.green;
shape.setColor(c);
}
if (s.equals("Orange")) {
c = Color.orange;
shape.setColor(c);
}
if (s.equals("Cyan")) {
c = Color.cyan;
shape.setColor(c);
}
if (s.equals("Dark Gray")) {
c = Color.darkGray;
shape.setColor(c);
}
if (s.equals("Gray")) {
c = Color.gray;
shape.setColor(c);
}
if (s.equals("Light Gray")) {
c = Color.lightGray;
shape.setColor(c);
}
if (s.equals("Magenta")) {
c = Color.magenta;
shape.setColor(c);
}
if (s.equals("Pink")) {
c = Color.pink;
shape.setColor(c);
}
if (s.equals("White")) {
c = Color.white;
shape.setColor(c);
}
if (s.equals("Banana Yellow")) {
c = new Color(227, 207, 87);
shape.setColor(c);
}
if (s.equals("Ivory Black")) {
c = new Color(88, 87, 86);
shape.setColor(c);
}
if (s.equals("Coral Red")) {
c = new Color(255, 127, 80);
shape.setColor(c);
}
if (s.equals("Black Red")) {
c = new Color(116, 0, 0);
shape.setColor(c);
}
}
}
public void send() {
try {
JSONObject test = new JSONObject();
test.put("command_name", "send");
os.writeUTF(test.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
//System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
}
public void paint(JSONObject command) {
if (command.get("operation").toString().equals("mousePressed")) {
startX = Integer.parseInt(command.get("startX").toString());
startY = Integer.parseInt(command.get("startY").toString());
text="";
} else if (command.get("operation").toString().equals("mouseReleased")) {
endX = Integer.parseInt(command.get("endX").toString());
endY = Integer.parseInt(command.get("endY").toString());
Graphics2D g2 = shape.getBufImage().createGraphics();
Color c1 = toColor(command.get("color").toString());
g2.setColor(c1);
String type = command.get("shape").toString();
drawUpdate(type, g2);
shape.repaint();
} else if (command.get("operation").toString().equals("mouseDragged")) {
startX = Integer.parseInt(command.get("startX").toString());
startY = Integer.parseInt(command.get("startY").toString());
endX = Integer.parseInt(command.get("endX").toString());
endY = Integer.parseInt(command.get("endY").toString());
Graphics2D g2 = shape.getBufImage().createGraphics();
Color c1 = toColor(command.get("color").toString());
g2.setColor(c1);
String type = command.get("shape").toString();
drawDragged(type, g2);
shape.repaint();
}
else if(command.get("operation").toString().equals("type")) {
String type = command.get("shape").toString(); //shape, text
Graphics2D g2 = shape.getBufImage().createGraphics();
text+=command.get("text").toString(); //text,"keyboardinput"
Color c1 = toColor(command.get("color").toString());
g2.setColor(c1);
drawText(type, g2,text);
shape.repaint();
}
}
private void drawDragged(String type, Graphics2D g2) {
switch (type) {
case "eraser":
// g2.setColor(Color.white);
g2.setColor(new Color(254, 255, 255));// similar to white
g2.setStroke(new BasicStroke(20));
g2.drawLine(startX, startY, endX, endY);
g2.setColor(this.c);
break;
case "pen":
// how to implement the pen func
g2.drawLine(startX, startY, endX, endY);
break;
default:
break;
}
}
private void drawText(String type, Graphics2D g2,String text) {
g2.drawString(text,startX,startY);
}
public void drawUpdate(String object, Graphics2D g2) {
switch (object) {
case "line":
g2.drawLine(startX, startY, endX, endY);
break;
case "rectangle":
if(startX<endX&&startY<endY) {
g2.drawRect(startX, startY, endX - startX,endY - startY);
}else if(startX<endX&&startY>endY) {
g2.drawRect(startX, endY, endX - startX,startY-endY);
}else if(startX>endX&&startY<endY) {
g2.drawRect(endX, startY,startX-endX,endY - startY);
//System.out.println("1111");
}else if(startX>endX&&startY>endY) {
g2.drawRect(endX, endY,startX-endX,startY-endY);
}
break;
case "circle":
if(startX<endX&&startY<endY) {
g2.drawArc(startX, startY, endX - startX, endX - startX, 0, 360);
}else if(startX<endX&&startY>endY) {
g2.drawArc(startX, endY, endX - startX, endX - startX, 0, 360);
}else if(startX>endX&&startY<endY) {
g2.drawArc(endX, startY, startX-endX, startX-endX, 0, 360);
}else if(startX>endX&&startY>endY){
g2.drawArc(endX, endY, startY-endY, startY-endY, 0, 360);//weired
//System.out.println("11111");
}
break;
case "oval":
if(startX<endX&&startY<endY) {
g2.drawOval(startX, startY, endX - startX, endY - startY);
}else if(startX<endX&&startY>endY) {
g2.drawOval(startX, endY, endX - startX,startY-endY);
}else if(startX>endX&&startY<endY) {
g2.drawOval(endX, startY, startX-endX,endY - startY);
}else if(startX>endX&&startY>endY){
g2.drawOval(endX, endY, startX-endX, startY-endY);
//System.out.println("11111");
}
break;
default:
break;
}
}
public static Color toColor(String str) {
int i = Integer.parseInt(str.substring(1), 16);
return new Color(i);
}
public String convertToString(int[][] array, int row, int col) {
String str = "";
String newStr = null;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
newStr = String.valueOf(array[i][j]);
str = str + newStr + ",";
}
}
return str;
}
public int[][] convertToArray(String str, int row, int col) {
int[][] intArray = new int[row][col];
int count = 0;
String[] strArray = str.split(",");
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
intArray[i][j] = Integer.parseInt(strArray[count]);
++count;
}
}
return intArray;
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
String str=String.valueOf(e.getKeyChar());
shape.setText(str);
// System.out.println(str);
Graphics2D ga =shape.getBufImage().createGraphics();
JSONObject command = new JSONObject();
command.put("command_name", "draw");
command.put("operation", "type");
command.put("shape", "text");
command.put("text", str);
command.put("color",shape.getColor());
command.put("which", "0");
try {
os.writeUTF(command.toString());
os.flush();
} catch (Exception e1) {
// TODO Auto-generated catch block
//System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection Lost! Closing thhe application");
System.exit(0);
}
shape.drawText(ga);
//this.repaint();
shape.repaint();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
JSONObject newCommand = new JSONObject();
newCommand = new JSONObject();
newCommand.put("command_name", "close");
try {
os.writeUTF(newCommand.toJSONString());
os.flush();
os.close();
manager.is.close();
manager.client.close();
} catch (Exception e2) {
// System.out.println("Connection Lost! Please close the application.");
JOptionPane.showMessageDialog(null, "Connection lost! Closing the application.");
System.exit(0);
} finally {
System.exit(0);
}
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
}
class ComboBoxRenderer extends JPanel implements ListCellRenderer
{
private static final long serialVersionUID = -1L;
private Color[] colors;
private String[] strings;
JPanel textPanel;
JLabel text;
public ComboBoxRenderer(JComboBox combo) {
textPanel = new JPanel();
textPanel.add(this);
text = new JLabel();
text.setOpaque(true);
text.setFont(combo.getFont());
textPanel.add(text);
}
public void setColors(Color[] col)
{
colors = col;
}
public void setStrings(String[] str)
{
strings = str;
}
public Color[] getColors()
{
return colors;
}
public String[] getStrings()
{
return strings;
}
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
if (isSelected)
{
setBackground(list.getSelectionBackground());
}
else
{
setBackground(Color.WHITE);
}
if (colors.length != strings.length)
{
System.out.println("colors.length does not equal strings.length");
return this;
}
else if (colors == null)
{
System.out.println("use setColors first.");
return this;
}
else if (strings == null)
{
System.out.println("use setStrings first.");
return this;
}
text.setBackground(getBackground());
text.setText(value.toString());
if (index>-1) {
text.setForeground(colors[index]);
}
return text;
}
}