1212import javax .swing .*;
1313import javax .swing .table .DefaultTableCellRenderer ;
1414import org .apache .commons .collections15 .MapIterator ;
15+ import org .apache .commons .collections15 .Transformer ;
1516import org .apache .commons .collections15 .map .HashedMap ;
1617
1718import edu .uci .ics .jung .algorithms .shortestpath .DijkstraShortestPath ;
@@ -34,8 +35,7 @@ public class PrintingFrame extends JFrame {
3435 String [] ends ;
3536 int choice = 0 ;
3637
37- public PrintingFrame (int choice ) {
38-
38+ private void setView () {
3939 graph = GraphPanel .getGraph ();
4040 setLayout (null );
4141
@@ -55,6 +55,11 @@ public PrintingFrame(int choice) {
5555 cycle .setFont (new Font (Font .DIALOG , Font .BOLD , 12 ));
5656 cycle .setName ("cycle" );
5757
58+ }
59+
60+ public PrintingFrame (int choice ) {
61+
62+ setView ();
5863 this .choice = choice ;
5964 if (choice == 1 )
6065 adjacencyList ();
@@ -76,25 +81,65 @@ else if (choice == 10)
7681 minimumHamiltonian ();
7782 else if (choice == 6 )
7883 coloringProblem ();
79- else if (choice == 12 )
80- maxFlow ();
81- else if (choice == 13 )
82- dijkstra ();
8384
8485 }
8586
86- private void dijkstra () {
87- System .out .println ("dijsktra" );
88- DijkstraShortestPath x = new DijkstraShortestPath <String ,String >(graph );
89- ArrayList out = new ArrayList ();
90- for (int i = 0 ; i < out .size (); ++i ){
91- System .out .println (out .get (i ));
87+ public PrintingFrame (int choice , String from , String to ) {
88+ setView ();
89+ this .choice = choice ;
90+ if (choice == 12 )
91+ maxFlow (from , to );
92+ else if (choice == 13 )
93+ dijkstra (from , to );
94+ }
95+
96+ private void dijkstra (String from , String to ) {
97+ msg = new Label ("Cost from " + from + " to " + to + " = " );
98+ this .setTitle ("Dijkstra Algorithm" );
99+ msg .setBounds (80 , 20 , 5000 , 30 );
100+ msg .setFont (new Font (Font .MONOSPACED , Font .BOLD , 25 ));
101+ add (msg );
102+
103+ if (graph .getVertexCount () == 0 ) {
104+ msg .setText ("The Graph Has No Vertices" );
105+ msg .setBounds (90 , 20 , 400 , 30 );
106+ return ;
92107 }
93108
94-
109+ if (!graph .containsVertex (from ) || !graph .containsVertex (to )) {
110+ msg .setText ("Invalid Source Or Destination" );
111+ return ;
112+ }
113+
114+ Transformer <String , Number > nev = new Transformer <String , Number >() {
115+ public Number transform (String arg0 ) {
116+ String cost = arg0 .trim ();
117+ if (cost == null || cost .equals ("" )) {
118+ return 0 ;
119+ } else {
120+ if (arg0 .contains ("." ))
121+ return Double .valueOf (arg0 .trim ());
122+ else
123+ return Integer .valueOf (arg0 .trim ());
124+ }
125+ }
126+ };
127+ DijkstraShortestPath x = new DijkstraShortestPath <String , String >(graph , nev );
128+ List out = x .getPath (from , to );
129+ if (out .size () == 0 ) {
130+ msg .setText ("Can't find a path form Source to Destination" );
131+ msg .setFont (new Font (Font .MONOSPACED , Font .BOLD , 18 ));
132+ msg .setBounds (50 , 20 , 500 , 30 );
133+ return ;
134+ }
135+ msg .setText (msg .getText () + " " + x .getDistance (from , to ));
136+ for (int i = 0 ; i < out .size (); ++i ) {
137+ System .out .print (graph .getEndpoints (out .get (i ).toString ()).toArray ()[0 ] + " " );
138+ System .out .println (graph .getEndpoints (out .get (i ).toString ()).toArray ()[1 ]);
139+ }
95140 }
96-
97- private void maxFlow () {
141+
142+ private void maxFlow (String from , String to ) {
98143 System .out .println ("maxFlow" );
99144
100145 }
0 commit comments