Skip to content

Commit 47532e0

Browse files
committed
Printed Dijkstra output
1 parent 2b0ef64 commit 47532e0

File tree

8 files changed

+66
-15
lines changed

8 files changed

+66
-15
lines changed

bin/controller/Home.class

246 Bytes
Binary file not shown.

bin/gui/InputFrame2$mouse.class

104 Bytes
Binary file not shown.

bin/gui/PrintingFrame$1.class

1.34 KB
Binary file not shown.

bin/gui/PrintingFrame$listen.class

0 Bytes
Binary file not shown.

bin/gui/PrintingFrame.class

1.36 KB
Binary file not shown.

src/controller/Home.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ public static void updateFrame() {
1717
home.copy(new PrintingFrame(choice));
1818
}
1919

20+
public static void updateFrameWithSrcAndDest(String src, String dest) {
21+
home.copy(new PrintingFrame(choice, src, dest));
22+
}
23+
2024
}

src/gui/InputFrame2.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ else if (event.getComponent().getName().equals("deleteEdge")) {
188188
deleteETo.setText("");
189189
} else if (event.getComponent().getName().equals("submit")) {
190190
Home.updateFrame();
191+
Home.updateFrameWithSrcAndDest(src.getText().trim(), dest.getText().trim());
192+
191193
} else {
192194
if (Home.choice >= 1 && Home.choice <= 4) {
193195
Home.home.copy(new TypeFrame("Select"));

src/gui/PrintingFrame.java

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import javax.swing.*;
1313
import javax.swing.table.DefaultTableCellRenderer;
1414
import org.apache.commons.collections15.MapIterator;
15+
import org.apache.commons.collections15.Transformer;
1516
import org.apache.commons.collections15.map.HashedMap;
1617

1718
import 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

Comments
 (0)