-
Notifications
You must be signed in to change notification settings - Fork 1
/
testing_Dijkstra.java
101 lines (82 loc) · 3.15 KB
/
testing_Dijkstra.java
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
//Class for testing Dijkstra's algorithm for flight routing (Ongoing)
//Uses priority queue that is absent in BFS
//Part of Final Project - for MOOC on Graph Theory, UCSD on Coursera
//******************************************************************
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class DijkstraTester
{
public static void main(String args[])
{
try
{
File cityPairs = new File("city_pairs.txt"); //Import files
File cityxy = new File("city_codes.txt");
Dijkstra d = new Dijkstra(cityPairs);
int width = 700; //Create dimensions for component
int height = 500;
mapComponent map = new mapComponent(cityxy, d, width, height);
map.setPreferredSize(new Dimension(width,height));
JScrollPane mapPane = new JScrollPane(map);
JButton find = new JButton("Get path"); //Create button to make path
find.addActionListener(new DijkstraListener(start, end, map, d, distance));
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(start);
panel.add(end);
panel.add(find);
panel.add(distance);
}
catch(FileNotFoundException e)
{
System.out.println("Incorrect file name(s).");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Two files are required as command line arguments");
}
}
public static class DijkstraListener implements ActionListener //Action listener subclass
{
private JComboBox start;
private JComboBox end;
private mapComponent map;
private Dijkstra d;
private JTextArea distance;
public DijkstraListener(JComboBox s, JComboBox e, mapComponent m, Dijkstra d, JTextArea dist) //Initialize
{
start = s;
end = e;
map = m;
this.d = d;
distance = dist;
}
public void actionPerformed(ActionEvent ae) //Displays shortest path
{
if(ae.getActionCommand().equals("Get path"))
{
String startCity = (String) start.getSelectedItem(); //Get start and end city
String endCity = (String) end.getSelectedItem();
if(!startCity.equals(endCity)) //Only display path if going to a different city
{
d.findPath(startCity);
d.setPath(endCity);
ArrayList<Vertex> path = d.getPath();
map.addPath(path); //Paint the component
map.repaint();
String theDistance = String.valueOf(d.getDistance(endCity)); //Display the distance
distance.setText(theDistance.substring(0,6) + " Miles");
}
else //No distance needs to be travelled if reached destination
{
distance.setText("00.00 Miles");
ArrayList<Vertex> path = null;
map.addPath(path);
map.repaint();
}
}
}
}