-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.java
More file actions
889 lines (757 loc) · 33.8 KB
/
Copy pathDisplay.java
File metadata and controls
889 lines (757 loc) · 33.8 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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Klasse Knoten.
* für die Darstellungen eines Graphen mittels Adjazenzmatrix
* auf der Basis des Programms von O.Zimmermann (Benötigt mindestens die Klasse Knoten)
*
* @author F.Paul & J.S.Dschungelskog
* @version 1.0.4
*
* @source https://github.com/Info-LK-Joe-Simon/Graph-Visualization
*/
public class Display extends Thread {
private JFrame frame;
private Input input;
private Graphics graphics, g;
private BufferedImage image;
private String versionInfo="";
// Create a menu bar
JMenuBar menuBar = new JMenuBar();
JMenu graphMenuBar = new JMenu("Graph");
JMenuItem saveGraphItem = new JMenuItem("Save");
JMenu knotMenuBar = new JMenu("Knot");
JMenuItem knotMarkItem = new JMenuItem("Mark Knot");
JMenuItem knotUnmarkItem = new JMenuItem("Unmark Knot");
JMenu edgeMenuBar = new JMenu("Edge");
JMenuItem edgeMarkItem = new JMenuItem("Mark Edge");
JMenuItem edgeUnmarkItem = new JMenuItem("Unmark Edge");
JMenu aboutMenuBar = new JMenu("About");
JMenuItem aboutCreditsItem = new JMenuItem("Credits");
JMenuItem aboutGithubItem = new JMenuItem("Source Code");
JMenuItem aboutCheckForUpdatesItem = new JMenuItem("Check for Updates");
JLabel versionLabel=new JLabel("Version unknown");
private int width, height;
private int[] oldMousePos={-1,-1};
private double[] pos={0.0,0.5};
private double zoom=1.0;
private double[][] adjazenzmatrix;
private double[][] laplacematrix;
private int[] listByLeastConncection;
private Knot[] knots;
private Knoten[] knoten=null;
private boolean undecorated=false;
private boolean darkmode=false;
private Color c_white=Color.WHITE;
private Color c_black=Color.BLACK;
private Color c_orange=Color.ORANGE;
private Color c_green=Color.GREEN;
private Color c_magenta=Color.MAGENTA;
private float knot_radius=25;
private float selectedKnotRadius=2;
private Knot currentDraggedKnot=null;
private ArrayList<Integer> listOfSelectedKnots = new ArrayList<Integer>();;
private boolean printWeight=true;
private boolean fillKnots=true;
private int decimalPlaces = 2;
private String localFilePathOfVersion="./src/Display.java";
private String gitHubFilePathOfVersion="https://github.com/Info-LK-Joe-Simon/Graph-Visualization/blob/main/Display.java";
public Display(int w, int h, boolean autostart){
initilize(w, h, null, autostart);
}
public Display(int w, int h){
initilize(w, h, null,true);
}
public Display(){
initilize(800, 600, null,true);
}
public Display(int w, int h, double[][] adjazenzmatrix, boolean autostart){
initilize(w, h, adjazenzmatrix, autostart);
}
public Display(int w, int h, double[][] adjazenzmatrix){
initilize(w, h, adjazenzmatrix,true);
}
public Display(double[][] adjazenzmatrix){
initilize(800, 600, adjazenzmatrix,true);
}
public void initilize(int w, int h, double[][] adjazenzmatrix, boolean autostart){
versionInfo=VersionChecker.check_version(localFilePathOfVersion, gitHubFilePathOfVersion);
width=w;
height=h;
frame= new JFrame();
//Interesting to play with you might set undecorated to true like:
//undecorated=true;
frame.setUndecorated(undecorated);
frame.setSize(new Dimension(width+16, height+30));
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
frame.setVisible(true);
frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
Image icon = Toolkit.getDefaultToolkit().getImage("./src/DisplayingGraphLogo.png");
frame.setIconImage(icon);
frame.setTitle("Graph - Visualisation");
frame.getContentPane().setBackground(Color.BLACK);
input=new Input();
frame.addKeyListener(input);
frame.addMouseListener(input);
frame.addMouseMotionListener(input);
frame.addMouseWheelListener(input);
frame.addFocusListener(input);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
g= frame.getGraphics();
g.setColor(darkmode?c_white:c_black);
g.setFont(new Font("Arial", Font.PLAIN, 32));
image=new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
graphics= image.getGraphics();
graphics.setColor(darkmode?c_white:c_black);
graphics.fillRect(0,0,image.getWidth(), image.getHeight());
graphics.setFont(new Font("Arial", Font.PLAIN, 32));
this.adjazenzmatrix=adjazenzmatrix;
//addMenuBar();
if (autostart)
this.start();
//credtis();
openMenuWindow();
}
public Input getInput(){
return input;
}
private void addMenuBar(JFrame f) {
saveGraphItem.addActionListener(e -> saveGraph());
graphMenuBar.add(saveGraphItem);
menuBar.add(graphMenuBar);
knotMarkItem.addActionListener(e -> markKnots(listOfSelectedKnots, true));
knotUnmarkItem.addActionListener(e -> markKnots(listOfSelectedKnots, false));
knotMenuBar.add(knotMarkItem);
knotMenuBar.add(knotUnmarkItem);
menuBar.add(knotMenuBar);
//edgeMarkItem.addActionListener(e -> markEdges(listOfSelectedKnots, true));
//edgeUnmarkItem.addActionListener(e -> markEdges(listOfSelectedKnots, false));
edgeMenuBar.add(edgeMarkItem);
edgeMenuBar.add(edgeUnmarkItem);
menuBar.add(edgeMenuBar);
aboutCreditsItem.addActionListener(e -> credtis());
aboutGithubItem.addActionListener(e -> {
try {
Desktop.getDesktop().browse(new URI("https://github.com/Info-LK-Joe-Simon/Graph-Visualization/blob/main/"));
} catch (Exception ex) {
ex.printStackTrace();
}
});
aboutCheckForUpdatesItem.addActionListener(e -> {
versionInfo=VersionChecker.check_version(localFilePathOfVersion, gitHubFilePathOfVersion);
versionLabel.setText(versionInfo);
});
aboutMenuBar.add(aboutCreditsItem);
aboutMenuBar.add(aboutGithubItem);
aboutMenuBar.add(aboutCheckForUpdatesItem);
menuBar.add(aboutMenuBar);
f.setJMenuBar(menuBar);
f.revalidate();
f.repaint();
}
public void switchToDarkMode() {
darkmode = !darkmode;
frame.getContentPane().setBackground(darkmode ? c_black : c_white);
graphics.setColor(darkmode ? c_white : c_black);
g.setColor(darkmode ? c_white : c_black);
frame.repaint();
}
public void setPrintWeight(boolean printWeight) {
this.printWeight = printWeight;
}
public boolean getPrintWeigth(){
return printWeight;
}
public void setFillKnots(boolean f){fillKnots=f;}
public boolean getFillKnots(){return fillKnots;}
private double[][] getLaplaceMatrix(double[][] a){
if(laplacematrix!=null)
return laplacematrix;
laplacematrix=new double[a.length][a[0].length];
for(int i=0; i<a.length; i++) {
int numOfConnections=0;
for (int j=0; j < a.length; j++) {
if (j != i && a[i][j] != 0)
numOfConnections++;
laplacematrix[i][j]=a[i][j];
}
laplacematrix[i][i]=numOfConnections;
}
return laplacematrix;
}
private void prepareKnots(double[][] a){
laplacematrix=getLaplaceMatrix(a);
if(knots==null)
knots = new Knot[a.length];
if(a.length!= knots.length)
knots = new Knot[a.length];
for(int i=0; i<a.length; i++){
if(knots[i]==null)
prepareKnot(a, Integer.toString(i), false, i);
}
}
private void prepareKnot(double[][] a, String name, boolean marked, int i){
float[] pos = getKnotPos(a, i);
if(knoten==null)
knots[i]=new Knot(pos, name, marked);
else if(knoten[i]==null)
knots[i]=new Knot(pos, name, marked);
else
knots[i]=new Knot(pos, knoten[i].getBezeichnung(), knoten[i].getMarkierung());
}
private float[] getKnotPos(double[][] a, int i){
float[] pos=new float[2];
int length=a.length;
if(length%2==0){
if(i==0)
return new float[]{width / 2, height / 2};
i-=1;
length-=1;
}
if(i%2!=0)
i=length-(i+1)/2;
else
i=i/2;
if(true) {
pos[0] = (float) (Math.sin(2 * Math.PI / length * i)) * width / 3 + width / 2;
pos[1] = (float) (Math.cos(2 * Math.PI / length * i)) * height / 3 + height / 2;
}
return pos;
//return new float[]{(float) (Math.random() * width), (float) (Math.random() * height)};
}
private void draw(){
update();
if (adjazenzmatrix!=null) {
prepareKnots(adjazenzmatrix);
drawGraph(adjazenzmatrix);
}
swapBuffers();
}
private void drawGraph(double[][] a){
for(int i=0; i < knots.length; i++)
for (int j = 0; j < adjazenzmatrix.length; j++)
if (adjazenzmatrix[i][j] != 0 || adjazenzmatrix[j][i] != 0)
drawLineBetweenKnots(i, j);
for(int i=0; i < knots.length; i++){
drawKnot(i);
}
}
private void drawKnot(int i){
if(listOfSelectedKnots.contains(i)) {
graphics.setColor(c_magenta);
graphics.fillOval((int) (knots[i].getX() - (knot_radius + selectedKnotRadius) + pos[0]), (int) (knots[i].getY() - (knot_radius + selectedKnotRadius) + pos[1]), (int) ((knot_radius + selectedKnotRadius) * 2), (int) ((knot_radius + selectedKnotRadius) * 2));
}
graphics.setColor(knots[i].marked?(darkmode ? c_green : c_orange):darkmode?c_white:c_black);
graphics.fillOval((int)(knots[i].getX()-knot_radius+pos[0]),(int)(knots[i].getY()-knot_radius+pos[1]),(int)(knot_radius*2),(int)(knot_radius*2));
graphics.setColor(fillKnots?knots[i].marked?(darkmode ? c_green : c_orange):darkmode?c_white:c_black:!darkmode?c_white:c_black);
graphics.fillOval((int)(knots[i].getX()-(knot_radius-1)+pos[0]),(int)(knots[i].getY()-(knot_radius-1)+pos[1]),(int)((knot_radius-1)*2),(int)((knot_radius-1)*2));
graphics.setColor((fillKnots?!darkmode:darkmode)?c_white:c_black);
graphics.setFont(new Font("Arial", Font.PLAIN, 12));
FontMetrics fontMetrics = graphics.getFontMetrics();
int nameWidth = fontMetrics.stringWidth(knots[i].getName());
int nameHeight = fontMetrics.getHeight();
graphics.drawString(
knots[i].getName(),
(int)(knots[i].getX() - nameWidth / 2 + pos[0]),
(int)(knots[i].getY() + nameHeight / 4 + pos[1])
);
}
private void drawLineBetweenKnots(int i, int j) {
if(adjazenzmatrix[i][j]==0&&adjazenzmatrix[j][i]==0)
return;
if(adjazenzmatrix[i][j] >= 0 && adjazenzmatrix[j][i] >= 0)
graphics.setColor(darkmode ? c_white : c_black);
else if(adjazenzmatrix[i][j] < 0 || adjazenzmatrix[j][i] < 0)
graphics.setColor(darkmode ? c_green : c_orange);
graphics.drawLine((int) (knots[i].getX()+pos[0]), (int) (knots[i].getY()+pos[1]), (int) (knots[j].getX()+pos[0]), (int) (knots[j].getY()+pos[1]));
if(!printWeight)
return;
int midX = (int) ((knots[i].getX() + knots[j].getX()) / 2 + pos[0]);
int midY = (int) ((knots[i].getY() + knots[j].getY()) / 2 + pos[1]);
graphics.setColor(darkmode ? c_white : c_black);
graphics.setFont(new Font("Arial", Font.PLAIN, 12));
FontMetrics fontMetrics = graphics.getFontMetrics();
if(i<=j)
if(Math.abs(adjazenzmatrix[i][j])==Math.abs(adjazenzmatrix[j][i])) {
String weight = Double.toString(Math.abs(roundToDecimalPlaces(adjazenzmatrix[i][j], decimalPlaces)));
int weightWidth = fontMetrics.stringWidth(weight);
int weightHeight = fontMetrics.getHeight();
graphics.drawString(weight, midX, midY - weightHeight/2);
}
else{
String weight = (adjazenzmatrix[i][j] == 0 ? "" : (knots[i].getX()>knots[j].getX()?"<- ":"-> ") + Math.abs(roundToDecimalPlaces(adjazenzmatrix[i][j], decimalPlaces))) + ((adjazenzmatrix[i][j] == 0 || adjazenzmatrix[j][i] == 0) ? "" : " | ") + (adjazenzmatrix[j][i] == 0 ? "" : (knots[i].getX()>knots[j].getX()?"-> ":"<- ") + Math.abs(roundToDecimalPlaces(adjazenzmatrix[j][i], decimalPlaces)));
int weightWidth = fontMetrics.stringWidth(weight);
int weightHeight = fontMetrics.getHeight();
graphics.drawString(weight, midX, midY - weightHeight / 2);
}
}
private void update(){
updateSize();
updatePos();
image=new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
graphics= image.getGraphics();
graphics.setColor(darkmode?c_black:c_white);
graphics.fillRect(0,0,image.getWidth(), image.getHeight());
}
private void updateSize(){
height=frame.getHeight();
width=frame.getWidth();
}
private void swapBuffers(){
g.drawImage(image, 0, 0, frame);
}
private void updatePos(){
if(input.inFocus){
if(input.getMouse(1)) {
mouseMoved();
}
else {
frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
currentDraggedKnot=null;
}
zoom+= input.getMouseWheel()/-50.0;
if(zoom<=0){
zoom=0;
}
}
oldMousePos[0] = input.getMouseX();
oldMousePos[1] = input.getMouseY();
}
private void mouseMoved(){
int i=0;
for(Knot knot : knots){
float[] knotPos = knot.getPos();
float distance = (float) Math.sqrt(Math.pow(oldMousePos[0] - (knotPos[0] + pos[0]), 2) + Math.pow(oldMousePos[1] - (knotPos[1] + pos[1]), 2));
if ((distance < (listOfSelectedKnots.contains(i)?knot_radius + selectedKnotRadius: knot_radius) && currentDraggedKnot == null) || currentDraggedKnot == knot) {
if (listOfSelectedKnots.isEmpty())
listOfSelectedKnots.add(i);
if (input.getKey(KeyEvent.VK_SHIFT) || input.getKey(KeyEvent.VK_CONTROL)) {
if (!listOfSelectedKnots.contains(i))
listOfSelectedKnots.add(i);
} else {
listOfSelectedKnots.clear();
listOfSelectedKnots.add(i);
}
currentDraggedKnot=knot;
knot.setX((float) (input.getMouseX() - pos[0]));
knot.setY((float) (input.getMouseY() - pos[1]));
frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
return;
}
i++;
}
listOfSelectedKnots.clear();
currentDraggedKnot = null;
pos[0] += (input.getMouseX() - oldMousePos[0]);
pos[1] += (input.getMouseY() - oldMousePos[1]);
frame.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
public void setDecimalPlaces (int dp){
decimalPlaces=dp;
}
public int getDecimalPlaces (){
return decimalPlaces;
}
public void setKnoten(Knoten[] k){
knoten=k;
}
public Knoten[] getKnoten(){
return knoten;
}
public void renameKnot(int index, String name){
if(knots==null) {
System.out.println("Warning: Knots array is null. If this is unexpected, try waiting for the thread to initialize. :)");
return;
}
if(index<knots.length)
knots[index].setName(name);
else
System.out.printf("Error: Attempted to access index %d, but it is out of bounds of Knots. Valid indices are between 0 and %d.%n", index, knots.length - 1);
}
public void renameKnots(ArrayList<Integer> indices, String name){
for (int index : indices) {
renameKnot(index, name);
}
}
public void renameKnots(int[] indices, String name){
for (int index : indices) {
renameKnot(index, name);
}
}
public void markKnot(int index, boolean marked){
if(knoten!=null&&knoten[index]!=null&&marked) //Unfortunately, there is no option to demark a knot in O.Zimmermanns Knoten.
knoten[index].setMarkierung();
if(knots==null) {
System.out.println("Warning: Knots array is null. If this is unexpected, try waiting for the thread to initialize. :)");
return;
}
if(index<knots.length)
knots[index].setMarked(marked);
else
System.out.printf("Error: Attempted to access index %d, but it is out of bounds of Knots. Valid indices are between 0 and %d.%n", index, knots.length - 1);
}
public void markKnots(ArrayList<Integer> indices, boolean marked){
for (int index : indices) {
markKnot(index, marked);
}
}
public void markKnots(int[] indices, boolean marked){
for (int index : indices) {
markKnot(index, marked);
}
}
public void printAdjazenzMatrix() {
if (adjazenzmatrix == null || adjazenzmatrix.length == 0) {
System.out.println("The adjacency matrix is empty.");
return;
}
System.out.println("Adjacency Matrix:");
for (int i = 0; i < adjazenzmatrix.length; i++) {
for (int j = 0; j < adjazenzmatrix[i].length; j++) {
System.out.print(adjazenzmatrix[i][j] + "\t");
}
System.out.println();
}
}
public void run(){
while (true) {
draw();
if(input.getKey(KeyEvent.VK_ESCAPE))
executeOrder66();
}
}
private void executeOrder66() {System.exit(0);}
public void setAdjazenzmatrix(double[][] a){adjazenzmatrix=a;}
public double[][] getAdjazenzmatrix(){return adjazenzmatrix;}
private static double roundToDecimalPlaces(double value, int decimalPlaces) {
double factor = Math.pow(10, decimalPlaces);
return Math.round(value * factor) / factor;
}
public class Knot{
private float[] pos = new float[2];
private String name="";
private boolean marked=false;
public Knot(float[] pos, String name, boolean marked){
this.pos=pos;
this.name=name;
this.marked=marked;
}
public void setPos(float[] pos) {
this.pos = pos;
}
public void setX(float x) {
this.pos[0] = x;
}
public void setY(float y) {
this.pos[1] = y;
}
public void setName(String name) {
this.name = name;
}
public void setMarked(boolean marked) {
this.marked = marked;
}
public float[] getPos() {
return pos;
}
public float getX(){
return pos[0];
}
public float getY(){
return pos[1];
}
public String getName() {
return name;
}
public boolean getMarked() {
return marked;
}
}
public void credtis() {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Credits");
Image icon = Toolkit.getDefaultToolkit().getImage("./src/DisplayingGraphLogo.png");
frame.setIconImage(icon);
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Create an instance of the CreditsPanel class
CreditsPanel creditsPanel = new CreditsPanel(frame);
frame.add(creditsPanel);
// Set frame properties
frame.setSize(800, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// Start the scrolling animation
creditsPanel.startAnimation();
});
}
private void saveGraph() {
String[] options = {"Save as CSV", "Save as JPG"};
int choice = JOptionPane.showOptionDialog(
frame,
"Choose save format:",
"Save Graph",
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
options,
options[0]
);
switch (choice) {
case 0:
try (FileWriter writer = new FileWriter("graph.csv")) {
String csvFileString="";
csvFileString+=",";
for (int j = 0; j < adjazenzmatrix[0].length; j++)
csvFileString+=knots[j].getName()+",";
csvFileString+="\n";
for (int i = 0; i < adjazenzmatrix.length; i++) {
csvFileString+=knots[i].getName()+",";
for (int j = 0; j < adjazenzmatrix[i].length; j++) {
csvFileString+=adjazenzmatrix[i][j]!=0?String.valueOf(adjazenzmatrix[i][j]):"";
if (j < adjazenzmatrix[i].length - 1)
csvFileString+=","; // other versions might need ";"
}
csvFileString+="\n";
}
writer.write(csvFileString);
JOptionPane.showMessageDialog(frame, "Graph saved as CSV successfully!");
} catch (IOException e) {
e.printStackTrace();
}
break;
case 1:
try {
File outputfile = new File("graph.jpg");
ImageIO.write(image, "jpg", outputfile);
JOptionPane.showMessageDialog(frame, "Graph saved as JPG successfully!");
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
break;
}
}
class CreditsPanel extends JPanel {
private class Star{
private double[] velocity = new double[2];
private double[] pos = new double[2];
public Star(double speed_factor){
velocity[0]=(Math.random()-0.5)*speed_factor;
velocity[1]=(Math.random()-0.5)*speed_factor;
pos[0]=0.5;
pos[1]=0.5;
}
public void update(){
pos[0]+=velocity[0];
pos[1]+=velocity[1];
if(pos[0]<0||pos[0]>1||pos[1]<0||pos[1]>1){
pos[0]=0.5;
pos[1]=0.5;
}
}
}
private int num_of_stars=100;
private Star[] stars = new Star[num_of_stars];
private String monologue = "GRAPH\n" +
"WARS\n" +
"\n" +
"A long time ago in a galaxy far, far away...\n" +
"An Info LK from Zimmermann fought an endless battle\n" +
"against the \"Zentralabitur\" and its brutal regulations\n" +
"and operators.\n" +
"\n" +
"But a group of students did not stop resisting the intruders.\n" +
"Among them were the most brilliant minds of the Galaxy,\n" +
"a team of cunning and resourceful beings who mastered all forms of logic and code.\n" +
"\n" +
"They were led by the fearless Joe Simon Dschungelskog,\n" +
"a student with the wisdom of the ancients and the will of a thousand warriors.\n" +
"\n" +
"Together, they built their resistance, using algorithms as their weapons,\n" +
"and data structures as their shields.\n" +
"Their knowledge of sorting and searching algorithms was unmatched,\n" +
"and they fought bravely to crack the complex system of the Zentralabitur.\n" +
"\n" +
"The battle raged on, with firewalls and encryption schemes falling one by one.\n" +
"They planted viruses of rebellion deep within the Ministry's database,\n" +
"disrupting their cruel plans and rewriting the very laws of assessment.\n" +
"\n" +
"In the final hours of the conflict, the forces of the Zentralabitur\n" +
"faced an unexpected defeat. The students had forged an unbreakable\n" +
"alliance under the leadership of Joe.\n" +
"\n" +
"The battle was won, and the brutal system of the Zentralabitur was no more.\n" +
"They had triumphed through their intellect, their unity, and their unwavering resolve.\n" +
"Now, the galaxy would be free to learn, create,\n" +
"and explore without the shackles of standardized testing.\n" +
"\n" +
"The rebellion had succeeded.\n" +
"And thus, a new era began.\n";
private float scrollSpeed = 1.5F;
private Timer timer;
private float yOffset;
private JFrame frame;
public CreditsPanel(JFrame f) {
setBackground(Color.BLACK);
setForeground(Color.YELLOW);
frame=f;
setFont(new Font("Arial", Font.PLAIN, 20));
this.grabFocus();
for(int i=0; i<num_of_stars; i++)
stars[i]=new Star(0.01);
}
public void startAnimation() {
timer = new Timer(20, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
yOffset -= scrollSpeed;
for(int i=0; i<num_of_stars; i++)
stars[i].update();
repaint();
}
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
for(int i=0; i<num_of_stars; i++)
g2d.drawLine((int)(stars[i].pos[0]*getWidth()), (int)(stars[i].pos[1]*getHeight()), (int)(stars[i].pos[0]*getWidth()), (int)(stars[i].pos[1]*getHeight()));
g2d.setColor(getForeground());
String[] monologue_array=monologue.split("\n");
for (int i=0; i<monologue_array.length; i++) {
String m=monologue_array[i];
// Set the font for the monologue text
FontMetrics fontMetrics = g2d.getFontMetrics();
int textWidth = fontMetrics.stringWidth(m);
int textHeight = fontMetrics.getHeight();
// Draw the monologue text at the calculated position
int x = (getWidth() - textWidth) / 2;
int y = (int)(getHeight() + yOffset + textHeight*i);
g2d.drawString(m, x, y);
// Draw the text again to handle the case when it goes off the screen
if (getHeight() + yOffset + textHeight * monologue_array.length < 0) {
frame.setVisible(false);
frame.dispose();
}
}
}
}
public class MenuWindow extends JFrame {
public MenuWindow(Display display) {
Image icon = Toolkit.getDefaultToolkit().getImage("./src/DisplayingGraphLogo.png");
setIconImage(icon);
setTitle("Menu");
setSize(400, 200);
setDefaultCloseOperation(undecorated?WindowConstants.EXIT_ON_CLOSE:WindowConstants.DO_NOTHING_ON_CLOSE);
setLayout(new BorderLayout());
addMenuBar(this);
versionLabel = new JLabel(VersionChecker.getVersionInfo());
versionLabel.setHorizontalAlignment(SwingConstants.LEFT);
JPanel versionPanel = new JPanel(new BorderLayout());
versionPanel.add(versionLabel, BorderLayout.WEST);
add(versionPanel, BorderLayout.NORTH);
JCheckBox darkModeCheckbox = new JCheckBox("Enable Dark Mode");
darkModeCheckbox.setSelected(display.darkmode);
darkModeCheckbox.addActionListener(e -> display.switchToDarkMode());
JCheckBox fillKnotsCheckbox = new JCheckBox("Fill Knots");
fillKnotsCheckbox.setSelected(display.fillKnots);
fillKnotsCheckbox.addActionListener(e -> fillKnots=!fillKnots);
JPanel settingsPanel = new JPanel();
settingsPanel.add(darkModeCheckbox);
settingsPanel.add(fillKnotsCheckbox);
add(settingsPanel, BorderLayout.CENTER);
JLabel footerLabel = new JLabel("<html>This service is brought to you by<br>Joe Simon D. & Fabian P.</html>");
footerLabel.setHorizontalAlignment(SwingConstants.LEFT);
JPanel footerPanel = new JPanel(new BorderLayout());
footerPanel.add(footerLabel, BorderLayout.WEST);
add(footerPanel, BorderLayout.SOUTH);
}
}
private void openMenuWindow() {
SwingUtilities.invokeLater(() -> {
MenuWindow settingsWindow = new MenuWindow(this);
settingsWindow.setVisible(true);
});
}
//Check for latest version
public class VersionChecker {
private static String versionInfo="Version is not checked yet";
public static String check_version(String localFilePath, String githubFileUrl) {
versionInfo ="";
try {
String localVersion = extractVersionFromLocalFile(localFilePath);
String githubVersion = fetchFileVersionFromGithub(githubFileUrl);
//Compare versions
if (localVersion == null) {
versionInfo ="Could not find @version in the local file.";
System.out.println(versionInfo);
return versionInfo;
}
if (githubVersion == null) {
versionInfo ="Could not find @version in the header.";
} else if (localVersion.equals(githubVersion)) {
versionInfo ="Version is up-to-date: " + localVersion;
} else {
versionInfo ="Version mismatch! Local: " + localVersion + ", GitHub: " + githubVersion +" \nPLEASE UPDATE";
}
} catch (Exception e) {
e.printStackTrace();
versionInfo ="Failed to check version.";
}
System.out.println(versionInfo);
return versionInfo;
}
// Extract the @version field from a file on the local system
private static String extractVersionFromLocalFile(String filePath) throws IOException {
StringBuilder content = new StringBuilder();
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = reader.readLine()) != null)
content.append(line).append("\n");
}
return extractVersionFromHeader(content.toString());
}
// Extract the @version field from a file on github
private static String fetchFileVersionFromGithub(String fileUrl) throws Exception {
URL url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
if (connection.getResponseCode() != 200)
throw new IOException("Failed to fetch file. HTTP error code: " + connection.getResponseCode());
StringBuilder content = new StringBuilder();
try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String inputLine;
while ((inputLine = in.readLine()) != null)
content.append(inputLine);
}
return extractVersionFromHeader(content.toString()); // Return file content
}
// Extract the @version field from the header comment
private static String extractVersionFromHeader(String content) {
// Regex to find @version followed by a version number
Pattern versionPattern = Pattern.compile("@version\\s+([\\d.]+(?:-\\w+)?)");
Matcher matcher = versionPattern.matcher(content);
return matcher.find()?matcher.group(1):null;
}
public static String getVersionInfo(){
return versionInfo;
}
}
}