20
20
import javafx .animation .Interpolator ;
21
21
import javafx .animation .ScaleTransition ;
22
22
import javafx .animation .SequentialTransition ;
23
+ import javafx .animation .StrokeTransition ;
23
24
import javafx .beans .value .ChangeListener ;
24
25
import javafx .beans .value .ObservableValue ;
25
26
import javafx .event .ActionEvent ;
35
36
import javafx .scene .control .TextInputDialog ;
36
37
import javafx .scene .control .ToggleGroup ;
37
38
import javafx .scene .effect .BlendMode ;
39
+ import javafx .scene .image .Image ;
40
+ import javafx .scene .image .ImageView ;
38
41
import javafx .scene .input .MouseButton ;
39
42
import javafx .scene .input .MouseEvent ;
40
43
import javafx .scene .layout .Pane ;
41
44
import javafx .scene .paint .Color ;
45
+ import javafx .scene .paint .Paint ;
42
46
import javafx .scene .shape .Circle ;
43
47
import javafx .scene .shape .Line ;
48
+ import javafx .scene .shape .Shape ;
44
49
import javafx .util .Duration ;
45
50
46
51
public class CanvasController implements Initializable , ChangeListener {
47
52
48
53
@ FXML
49
- private JFXButton canvasBackButton , clearButton , resetButton , gear ;
54
+ private JFXButton canvasBackButton , clearButton , resetButton , gear , playPauseButton ;
50
55
@ FXML
51
56
private JFXToggleButton addNodeButton , addEdgeButton , bfsButton , dfsButton , dijkstraButton , articulationPointButton ;
52
57
@ FXML
@@ -67,17 +72,22 @@ public class CanvasController implements Initializable, ChangeListener {
67
72
private JFXNodesList nodeList ;
68
73
@ FXML
69
74
private JFXSlider slider ;
75
+ @ FXML
76
+ private ImageView playPauseImage ;
70
77
71
78
int nNode = 0 , time = 500 ;
72
79
NodeFX selectedNode = null , articulationStart = null ;
73
80
List <NodeFX > circles = new ArrayList <NodeFX >();
81
+ List <Edge > mstEdges = new ArrayList <Edge >();
82
+ List <Shape > edges = new ArrayList <Shape >();
74
83
boolean addNode = true , addEdge = false , calculate = false ,
75
- calculated = false ;
84
+ calculated = false , playing = false , paused = false ;
76
85
List <Label > distances = new ArrayList <Label >(), visitTime = new ArrayList <Label >(), lowTime = new ArrayList <Label >();
77
86
private boolean weighted = Panel1Controller .weighted , unweighted = Panel1Controller .unweighted ,
78
87
directed = Panel1Controller .directed , undirected = Panel1Controller .undirected ,
79
88
bfs = true , dfs = true , dijkstra = true , articulationPoint = true ;
80
89
Algorithm algo = new Algorithm ();
90
+ SequentialTransition st ;
81
91
82
92
@ Override
83
93
public void initialize (URL url , ResourceBundle rb ) {
@@ -221,11 +231,15 @@ public void handle(MouseEvent mouseEvent) {
221
231
}
222
232
223
233
if (undirected ) {
224
- selectedNode .node .adjacents .add (new Edge (circle .node , Integer .valueOf (weight .getText ())));
225
- circle .node .adjacents .add (new Edge (selectedNode .node , Integer .valueOf (weight .getText ())));
234
+ Edge temp = new Edge (selectedNode .node , circle .node , Integer .valueOf (weight .getText ()), edgeLine );
235
+ mstEdges .add (temp );
236
+ selectedNode .node .adjacents .add (new Edge (selectedNode .node , circle .node , Integer .valueOf (weight .getText ()), edgeLine ));
237
+ circle .node .adjacents .add (new Edge (circle .node , selectedNode .node , Integer .valueOf (weight .getText ()), edgeLine ));
238
+ edges .add (edgeLine );
226
239
} else if (directed ) {
227
- selectedNode .node .adjacents .add (new Edge (circle .node , Integer .valueOf (weight .getText ())));
228
- circle .node .revAdjacents .add (new Edge (selectedNode .node , Integer .valueOf (weight .getText ())));
240
+ selectedNode .node .adjacents .add (new Edge (selectedNode .node , circle .node , Integer .valueOf (weight .getText ()), arrow ));
241
+ circle .node .revAdjacents .add (new Edge (circle .node , selectedNode .node , Integer .valueOf (weight .getText ()), arrow ));
242
+ edges .add (arrow );
229
243
}
230
244
}
231
245
if (addNode || (calculate && !calculated ) || addEdge ) {
@@ -285,7 +299,29 @@ public void handle(MouseEvent mouseEvent) {
285
299
}
286
300
287
301
};
288
-
302
+
303
+ @ FXML
304
+ public void PlayPauseHandle (ActionEvent event ){
305
+ System .out .println ("IN PLAYPAUSE" );
306
+ System .out .println (playing +" " + paused );
307
+ if (playing ){
308
+ Image image = new Image (getClass ().getResourceAsStream ("/play_arrow_black_48x48.png" ));
309
+ playPauseImage .setImage (image );
310
+ System .out .println ("Pausing" );
311
+ st .pause ();
312
+ paused = true ;
313
+ playing = false ;
314
+ return ;
315
+ }
316
+ if (paused ){
317
+ Image image = new Image (getClass ().getResourceAsStream ("/pause_black_48x48.png" ));
318
+ playPauseImage .setImage (image );
319
+ st .play ();
320
+ playing = true ;
321
+ paused = false ;
322
+ return ;
323
+ }
324
+ }
289
325
@ FXML
290
326
public void ResetHandle (ActionEvent event ) {
291
327
ClearHandle (null );
@@ -312,6 +348,8 @@ public void ResetHandle(ActionEvent event) {
312
348
dfsButton .setDisable (true );
313
349
dijkstraButton .setDisable (true );
314
350
articulationPointButton .setDisable (true );
351
+ playing = false ;
352
+ paused = false ;
315
353
}
316
354
317
355
@ FXML
@@ -352,6 +390,8 @@ public void ClearHandle(ActionEvent event) {
352
390
dfs = false ;
353
391
articulationPoint = false ;
354
392
dijkstra = false ;
393
+ playing = false ;
394
+ paused = false ;
355
395
}
356
396
357
397
@ FXML
@@ -492,7 +532,7 @@ public NodeFX(double x, double y, double rad, String name) {
492
532
*/
493
533
public class Algorithm {
494
534
495
- SequentialTransition st ;
535
+
496
536
497
537
//<editor-fold defaultstate="collapsed" desc="Dijkstra">
498
538
public void newDijkstra (Node source ) {
@@ -569,6 +609,7 @@ class Dijkstra {
569
609
});
570
610
st .onFinishedProperty ();
571
611
st .play ();
612
+ playing = true ;
572
613
//</editor-fold>
573
614
}
574
615
}
@@ -583,7 +624,7 @@ class BFS {
583
624
584
625
BFS (Node source ) {
585
626
586
- //<editor-fold defaultstate="collapsed" desc="Animation Initialize ">
627
+ //<editor-fold defaultstate="collapsed" desc="Set labels and distances ">
587
628
for (NodeFX n : circles ) {
588
629
distances .add (n .distance );
589
630
n .distance .setLayoutX (n .point .x + 20 );
@@ -614,13 +655,19 @@ class BFS {
614
655
for (Edge e : u .adjacents ) {
615
656
if (e != null ) {
616
657
Node v = e .target ;
658
+
617
659
if (!v .visited ) {
618
660
v .minDistance = u .minDistance + 1 ;
619
661
v .visited = true ;
620
662
q .push (v );
621
663
v .previous = u ;
622
664
623
665
//<editor-fold defaultstate="collapsed" desc="Animation Control">
666
+ //<editor-fold defaultstate="collapsed" desc="Change Edge colors">
667
+ StrokeTransition ftEdge = new StrokeTransition (Duration .millis (time ), e .line );
668
+ ftEdge .setToValue (Color .rgb (163 , 203 , 56 ,1.0 ));
669
+ st .getChildren ().add (ftEdge );
670
+ //</editor-fold>
624
671
FillTransition ft1 = new FillTransition (Duration .millis (time ), v .circle );
625
672
ft1 .setToValue (Color .FORESTGREEN );
626
673
ft1 .setOnFinished (ev -> {
@@ -646,12 +693,16 @@ class BFS {
646
693
ft1 .setToValue (Color .BLACK );
647
694
ft1 .play ();
648
695
}
696
+ for (Shape n : edges ){
697
+ n .setStroke (Color .BLACK );
698
+ }
649
699
FillTransition ft1 = new FillTransition (Duration .millis (time ), source .circle );
650
700
ft1 .setToValue (Color .RED );
651
701
ft1 .play ();
652
702
});
653
703
st .onFinishedProperty ();
654
704
st .play ();
705
+ playing = true ;
655
706
//</editor-fold>
656
707
}
657
708
}
@@ -691,12 +742,16 @@ class DFS {
691
742
ft1 .setToValue (Color .BLACK );
692
743
ft1 .play ();
693
744
}
745
+ for (Shape n : edges ){
746
+ n .setStroke (Color .BLACK );
747
+ }
694
748
FillTransition ft1 = new FillTransition (Duration .millis (time ), source .circle );
695
749
ft1 .setToValue (Color .RED );
696
750
ft1 .play ();
697
751
});
698
752
st .onFinishedProperty ();
699
753
st .play ();
754
+ playing = true ;
700
755
//</editor-fold>
701
756
}
702
757
@@ -716,8 +771,18 @@ public void DFSRecursion(Node source) {
716
771
v .visited = true ;
717
772
v .previous = source ;
718
773
// v.circle.distance.setText("Dist. : " + v.minDistance);
774
+ //<editor-fold defaultstate="collapsed" desc="Change Edge colors">
775
+ StrokeTransition ftEdge = new StrokeTransition (Duration .millis (time ), e .line );
776
+ ftEdge .setToValue (Color .FORESTGREEN );
777
+ st .getChildren ().add (ftEdge );
778
+ //</editor-fold>
719
779
DFSRecursion (v );
720
780
//<editor-fold defaultstate="collapsed" desc="Animation Control">
781
+ //<editor-fold defaultstate="collapsed" desc="Change Edge colors">
782
+ StrokeTransition ftEdge1 = new StrokeTransition (Duration .millis (time ), e .line );
783
+ ftEdge1 .setToValue (Color .BLUEVIOLET );
784
+ st .getChildren ().add (ftEdge1 );
785
+ //</editor-fold>
721
786
FillTransition ft1 = new FillTransition (Duration .millis (time ), v .circle );
722
787
ft1 .setToValue (Color .BLUEVIOLET );
723
788
ft1 .onFinishedProperty ();
@@ -782,6 +847,9 @@ class ArticulationPoint {
782
847
ft1 .setToValue (Color .BLACK );
783
848
ft1 .play ();
784
849
}
850
+ for (Shape n : edges ){
851
+ n .setStroke (Color .BLACK );
852
+ }
785
853
for (NodeFX n : circles ) {
786
854
if (n .node .isArticulationPoint ) {
787
855
FillTransition ft1 = new FillTransition (Duration .millis (time ), n );
@@ -792,6 +860,7 @@ class ArticulationPoint {
792
860
});
793
861
st .onFinishedProperty ();
794
862
st .play ();
863
+ playing = true ;
795
864
//</editor-fold>
796
865
}
797
866
@@ -823,6 +892,11 @@ void RecAP(Node s) {
823
892
if (!v .visited ) {
824
893
v .previous = s ;
825
894
childCount ++;
895
+ //<editor-fold defaultstate="collapsed" desc="Change Edge colors">
896
+ StrokeTransition ftEdge = new StrokeTransition (Duration .millis (time ), e .line );
897
+ ftEdge .setToValue (Color .FORESTGREEN );
898
+ st .getChildren ().add (ftEdge );
899
+ //</editor-fold>
826
900
RecAP (v );
827
901
828
902
s .lowTime = Math .min (s .lowTime , v .lowTime );
@@ -831,6 +905,11 @@ void RecAP(Node s) {
831
905
}
832
906
833
907
//<editor-fold defaultstate="collapsed" desc="Animation Control">
908
+ ///<editor-fold defaultstate="collapsed" desc="Change Edge colors">
909
+ StrokeTransition ftEdge1 = new StrokeTransition (Duration .millis (time ), e .line );
910
+ ftEdge1 .setToValue (Color .BLUEVIOLET );
911
+ st .getChildren ().add (ftEdge1 );
912
+ //</editor-fold>
834
913
FillTransition ft1 = new FillTransition (Duration .millis (time ), v .circle );
835
914
ft1 .setToValue (Color .BLUEVIOLET );
836
915
ft1 .setOnFinished (ev -> {
0 commit comments