TestDraw import java awt BorderLayout import java awt Color import jav

  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
А.1 TestDraw
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestDraw extends JFrame {
/**
* processor for working with csv file
*/
private static CSVProcessor proc;
/**
* path to csv-file
*/
public static String path = new String("d:\\Рабочий уголок\\ООП\\ІІ семестр\\Save\\curs\\test.csv");
public final static JTable table = new JTable();
public TestDraw() {
super("Пузиркова діаграма");
final DiagramDrawer drawer = new DiagramDrawer();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
proc = new CSVProcessor();
try {
proc.download(path);
} catch (FileNotFoundException e1) {
System.out.println("Exception 1: File Not Found!\n");
}
ArrayList<Bubble> bubbles = new ArrayList<Bubble>();
try {
bubbles = proc.parse();
drawer.setBubbles(bubbles);
} catch (CSVParseException e) {
e.printStackTrace();
}
setDefaultLookAndFeelDecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
TableModel model = new MyTableModel(bubbles);
table.setModel(model);
final JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
mainPanel.add(scrollPane, BorderLayout.WEST);
scrollPane.setPreferredSize(new Dimension(400, 600));
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
final JButton leftButton = new JButton("Зберегти зображення");
final JButton centerButton = new JButton("Запам'ятати стан");
final JButton rightButton = new JButton("Відновити стан");
leftButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawer.saveImage();
}
});
centerButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
proc.deparse(drawer.getBubbles());
proc.serialize();
try {
proc.upload(path);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
rightButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
proc.deserialize();
try{
proc.upload(path);
proc.download(path);
try {
drawer.setBubbles(proc.parse());
} catch (CSVParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1)
{
System.out.println("Exception 1.5: File Not Found!\n");
}
table.setModel(new MyTableModel(drawer.getBubbles()));
drawer.findScale();
repaint();
}
});
JPanel button_panel = new JPanel();
button_panel.setLayout(new FlowLayout());
button_panel.setPreferredSize(new Dimension(600, 45));
drawer.setPreferredSize(new Dimension(500, 500));
button_panel.add(leftButton);
button_panel.add(centerButton);
button_panel.add(rightButton);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.add(button_panel, BorderLayout.NORTH);
panel.add(drawer, BorderLayout.CENTER);
mainPanel.add(panel, BorderLayout.EAST);
getContentPane().add(mainPanel);
setPreferredSize(new Dimension(1000, 600));
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
new TestDraw();
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
А.2 Bubble
import java.awt.Color;
public class Bubble {
private double X;
private double Y;
private double R;
public boolean selected = false;
public Color color;
public Bubble(double X, double Y, double R, Color color){
this.X=X;
this.Y=Y;
this.R=R;
this.color = color;
}
public void setX(double X) {
this.X = X;
}
public double getX() {
return X;
}
public void setY(double Y) {
this.Y = Y;
}
public double getY() {
return Y;
}
public void setR(double R) {
this.R = R;
}
public double getR() {
return R;
}
}
А.3 DiagramDrawer
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
public class DiagramDrawer extends JPanel implements MouseListener, MouseMotionListener {
@Override
public void paint(Graphics g) {
super.paint(g);
draw(g);
}
/**
* pixel width
*/
private static int w = 500;
/**
* pixel height
*/
private static int h = 500;
/**
* scale
*/
private double scl = 1.0d;
/**
* bubble list
*/
private ArrayList<Bubble> bubbles;
/**
* get bubble list
*/
public ArrayList<Bubble> getList() {
return bubbles;
}
/**
* set new list
*/
public void setBubbles(ArrayList<Bubble> bubbles) {
this.bubbles = new ArrayList<Bubble>(bubbles);
}
/**
* construct
*/
public DiagramDrawer() {
super();
this.bubbles = new ArrayList<Bubble>();
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
/**
* get this list
*/
public ArrayList<Bubble> getBubbles() {
return bubbles;
}
/**
* find scale
*/
public void findScale() {
double maxX = bubbles.get(0).getX();
for (Bubble b : bubbles) {
if (b.getX() > maxX) {
maxX = b.getX();
}
}
double maxY = bubbles.get(0).getY();
for (Bubble b : bubbles) {
if (b.getY() > maxY) {
maxY = b.getY();
}
}
double maxR = bubbles.get(0).getR();
for (Bubble b : bubbles) {
if (b.getR() > maxR) {
maxR = b.getR();
}
}
double realWidth = maxX + 2 * maxR;
double realHeight = maxY + 2 * maxR;
if (realWidth > realHeight) {
scl = w / realWidth;
} else {
scl = h / realHeight;
}
}
/**
* draw circle
*
* @x coordinate
* @y coordinate
* @r radius
*/
private void drawCircle(double x, double y, double r, Graphics g,boolean selected,Color color) {
g.setColor(color);
Graphics2D g2d = (Graphics2D)g;
if(selected) {
g.setColor(Color.black);
g.drawOval((int)(x - r), (int)(y - r), (int)(r * 2), (int)(r * 2));
}
g.setColor(color);
g2d.fill(new Ellipse2D.Double(x - r, y - r, r * 2, r * 2));
g.setColor(Color.black);
g.drawLine((int)x, (int)y, (int)x, (int)y);
}
/**
* draw mrg
*/
private static int mrg = 30;
/**
* draw coordinate
*/
private void drawCoordinate(Graphics g) {
// coords
g.setColor(Color.black);
// oy
g.drawLine(mrg, mrg, mrg, h - mrg);
g.drawLine(mrg, mrg, mrg + 3, mrg + 10);
g.drawLine(mrg, mrg, mrg - 3, mrg + 10);
// ox
g.drawLine(mrg, h - mrg, w - mrg, h - mrg);
g.drawLine(w - mrg, h - mrg, w- mrg - 10, h - mrg - 3);
g.drawLine(w - mrg, h - mrg, w- mrg - 10, h - mrg + 3);
// end coord
int step = 70;
// у
for (int i = h - mrg-step; i > 0; i -= step) {
g.setColor(Color.gray);
g.drawLine(mrg - 3, i, w-mrg, i);
g.setColor(Color.lightGray);
g.drawLine(mrg - 3, i+step/2, w-mrg, i+step/2);
g.setColor(Color.black);
g.drawString(String.valueOf((int) ((h - mrg - i) / scl)),0, i+5);
}
// х
for (int i = mrg+step; i < w; i += step) {
g.setColor(Color.gray);
g.drawLine(i, mrg, i, h - mrg + 3);
g.setColor(Color.lightGray);
g.drawLine(i-step/2, mrg, i-step/2, h - mrg + 3);
g.setColor(Color.black);
g.drawString(String.valueOf((int) ((i-mrg) / scl)), i-5,h-mrg/4);
}
g.setColor(Color.black);
}
/**
* diagram image
*/
private BufferedImage image;
/**
* save image
* @name of image
*/
public void saveImage(){
JFileChooser filesave = new JFileChooser("d:/Рабочий уголок/ООП/ІІ семестр/Save/curs/");
FileNameExtensionFilter f = new FileNameExtensionFilter("jpg files","*.jpg","jpg");
filesave.addChoosableFileFilter(f);
filesave.setFileFilter(f);
int ret = filesave.showDialog(null, "Зберегти зображення");
if(ret==filesave.APPROVE_OPTION){
try {
ImageIO.write(image,"jpg",new File(filesave.getSelectedFile().getAbsolutePath()+".jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* drawing
*/
public void draw(Graphics g) {
if(sn==-1)findScale();
image = new BufferedImage(w, h,BufferedImage.TYPE_INT_RGB);
Graphics gImg = image.createGraphics();
gImg.setColor(Color.white);
gImg.fillRect(0, 0, w, h);
drawCoordinate(gImg);
for (Bubble b : bubbles) {
drawCircle(mrg + b.getX() * scl, h - b.getY() * scl - mrg, b.getR() * scl, gImg, b.selected, b.color);
}
g.drawImage(image, 50, 0, null);
}
private double findXY(double x1, double y1, double x2, double y2){
return (Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
/**
* check and select circle
*
*/
private int checkBubbles(double x, double y, int a){
int result = -1;
for (int i = 0; i < a;i++)
{
bubbles.get(i).selected = false;
}
for (int i = a; i < bubbles.size();i++)
{
bubbles.get(i).selected = false;
if(findXY(x,y,bubbles.get(i).getX(),bubbles.get(i).getY())<=bubbles.get(i).getR())
{
if (result==-1) {
bubbles.get(i).selected = true;
TestDraw.table.setRowSelectionInterval(i, i);
result = i;
}
}
}
return result;
}
/**
* select circle number
*
*/
public int sn = -1;
@Override
public void mouseClicked(MouseEvent arg0) {
double x = (arg0.getX()-mrg-50)/scl;
double y =(h-arg0.getY()-mrg)/scl;
if(arg0.getModifiers()==InputEvent.BUTTON1_MASK)
{
if(sn!=-1&& findXY(x,y,bubbles.get(sn).getX(),bubbles.get(sn).getY())>bubbles.get(sn).getR())sn=-1;
int a = checkBubbles(x,y, sn+1);
if(a==-1)sn = -1;
else sn = a;
}
if(arg0.getModifiers()==InputEvent.BUTTON3_MASK){
if(sn==-1){
Random rand = new Random();
Bubble b = new Bubble(x,y,w/scl/10,new Color(50 + rand.nextInt(205), 50 + rand.nextInt(205),50 + rand.nextInt(205), 130));
bubbles.add(b);
}
else {
bubbles.get(sn).setX(x);
bubbles.get(sn).setY(y);
}
}
if(arg0.getModifiers()==InputEvent.BUTTON2_MASK){
if(sn!=-1){
bubbles.remove(bubbles.get(sn));
sn = -1;
}
}
repaint();
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/**
* coordinate x for pressed mouse
*
*/
private double xP;
/**
* coordinate Y for pressed mouse
*
*/
private double yP;
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getModifiers()==InputEvent.BUTTON1_MASK){
xP = (arg0.getX()-mrg-50)/scl;
yP =(h-arg0.getY()-mrg)/scl;
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
findScale();
TestDraw.table.setModel(new MyTableModel(bubbles));
if(sn!=-1)TestDraw.table.setRowSelectionInterval(sn, sn);
repaint();
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getModifiers()==InputEvent.BUTTON1_MASK){
double x = (arg0.getX()-mrg-50)/scl;
double y =(h-arg0.getY()-mrg)/scl;
if(sn!=-1)
{
double dR = findXY(x,y,bubbles.get(sn).getX(),bubbles.get(sn).getY())-findXY(xP,yP,bubbles.get(sn).getX(),bubbles.get(sn).getY());
if(dR!=0) bubbles.get(sn).setR(bubbles.get(sn).getR()+dR/10);
}
repaint();
}
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
А.4 MyTableModel
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
public class MyTableModel implements TableModel {
private Set<TableModelListener> listeners = new HashSet<TableModelListener>();
private List<Bubble> bubbles;
public MyTableModel(List<Bubble> bubbles) {
this.bubbles = bubbles;
}
public void addTableModelListener(TableModelListener listener) {
listeners.add(listener);
}
public Class<?> getColumnClass(int columnIndex) {
return String.class;
}
public int getColumnCount() {
return 3;
}
public String getColumnName(int columnIndex) {
switch (columnIndex) {
case 0:
return "Координата Х";
case 1:
return "Координата У";
case 2:
return "Радіус";
}
return "";
}
public int getRowCount() {
return bubbles.size();
}
public Object getValueAt(int rowIndex, int columnIndex) {
Bubble bubble = bubbles.get(rowIndex);
switch (columnIndex) {
case 0:
return (int)bubble.getX();
case 1:
return (int)bubble.getY();
case 2:
return (int)bubble.getR();
}
return "";
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
public void removeTableModelListener(TableModelListener listener) {
listeners.remove(listener);
}
public void setValueAt(Object value, int rowIndex, int columnIndex) {
}
}
А.5 CSVProcessor
import java.awt.Color;
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class CSVProcessor {
/**
* constructor
*/
public CSVProcessor (){
list = new ArrayList<String>();
}
/**
* our list
*/
public ArrayList<String> list;
/**
* file path for (de)serialize
*/
private static String pathSer = new String("d:\\Рабочий уголок\\ООП\\ІІ семестр\\Save\\curs\\serialized.dat");
/**
* download strings from file to array
* @throws FileNotFoundException
*/
public void download(String path)throws FileNotFoundException{
File f = new File(path);
list.clear();
BufferedReader reader = new BufferedReader(new FileReader(f));
try{
String str;
while((str = reader.readLine())!= null) list.add(str);
} catch (IOException e2)
{
System.out.println("Exception 2: Can Not Read Line!");
} finally{
if(reader!=null)
try {
reader.close();
} catch (IOException e3) {
System.out.println("Exeption 3.1: File Can Not Close!");
}
}
}
/**
* upload strings from array to file
* @throws FileNotFoundException
*/
public void upload(String path)throws FileNotFoundException{
File f = new File(path);
f.getParentFile().mkdirs();
try {
f.createNewFile();
} catch (IOException e) {
System.out.println("can not create file");
e.printStackTrace();
}
PrintWriter pw = null;
try{
pw = new PrintWriter(f);
for (String str : list) {
pw.println(str);
}
}catch (IOException e){
}finally{
if(pw!=null) pw.close();
}
}
/**
* serialize to file
*/
public void serialize(){
ObjectOutputStream obout = null;
try {
obout = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(pathSer)));
} catch (IOException e6) {
System.out.println("Exception 6: Invalid Object Stream!");
}
try {
obout.writeObject(list);
} catch (IOException e7) {
System.out.println("Exception 7: Can Not Write Object!");
}
finally {
try {
obout.close();
} catch (IOException e8) {
System.out.println("Exception 8.1: Can Not Close Stream!");
}
}
}
/**
* deserialize from file
*/
public void deserialize(){
ObjectInputStream obinp = null;
try{
obinp = new ObjectInputStream(new BufferedInputStream(new FileInputStream(pathSer)));
@SuppressWarnings("unchecked")
ArrayList<String> temp = (ArrayList<String>) obinp.readObject();
list = temp;
} catch (FileNotFoundException e1) {
System.out.println("Exception 1.4: File Not Found!");
} catch (ClassNotFoundException e9) {
System.out.println("Exception 9: Class Not Found!");
} catch (IOException e10) {
System.out.println("Exception 10: Can Not Create Or Read Object!");
}
finally{
try {
obinp.close();
} catch (IOException e8) {
System.out.println("Exception 8.2: Can Not Close Stream!");
}
}
}
/**
* parsing data
*/
public ArrayList<Bubble> parse() throws CSVParseException{
Random rand = new Random();
int len = list.get(0).split(";").length;
if (len == 0) throw new CSVParseException("Not CSV Format!");
if (len !=3) throw new CSVParseException("Wrong column number!");
ArrayList<Bubble> bubbles = new ArrayList<Bubble>();
for (String str : list) {
String[] tokens = str.split(";");
if (tokens.length != 3)
throw new CSVParseException("Wrong line length!");
try
{
bubbles.add(new Bubble(Double.parseDouble(tokens[0]),Double.parseDouble(tokens[1]),
Double.parseDouble(tokens[2]),new Color(50 + rand.nextInt(205), 50 + rand.nextInt(205),50 + rand.nextInt(205), 130)));
}
catch(NumberFormatException e)
{
System.out.println("Not a number!");
}
}
return bubbles;
}
/**
* deparsing data
*/
public void deparse(ArrayList<Bubble> bubbles) {
list.clear();
for(Bubble b : bubbles)
list.add(b.getX()+";"+b.getY()+";"+b.getR());
}
/**
* return private pathSer
*/
public static String getPathSer (){
return pathSer;
}
/**
* add new string to list
* @param str string to add
*/
public void addString (String str){
list.add(str);
}
/**
* remove string from list
* @param num number of string to remove
*/
public void removeString (int num){
list.remove(num);
}
}
А.6 CSVParseException
public class CSVParseException extends Exception{
private static final long serialVersionUID = 1L;
public CSVParseException()
{
super();
}
public CSVParseException(String msg)
{
super(msg);
}
public CSVParseException(Throwable cause)
{
super(cause);
}
}