I am trying to implement dijkstras algorithm on a network of stations that is displayed in a graph in the GUI. I have included the code below which i am using to display the network (after being read from a .txt file). I have been looking at examples of dijkstra algorithm but every time im getting it wrong because i dont know how to apply it to my all ready setup graph. Just wondering if someone could show me how its done or point me towards one that would actually work for me.
CODE:
readButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (fileChooser.showOpenDialog(rootPane) == JFileChooser.APPROVE_OPTION) {
network = new TrainNetwork();
networkPanel.repaint();
firstStation = null;
sidePanel.listPanel.clearList();
File file = fileChooser.getSelectedFile();
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNext()) {
String type = scanner.next();
if (type.equalsIgnoreCase(STATION_STRING)) {
String stationName = scanner.next();
if (firstStation == null) {
firstStation = stationName;
}
network.addStation(stationName, scanner.nextInt(), scanner.nextInt());
} else if (type.equalsIgnoreCase(CONNECTION_STRING)) {
network.addConnection(scanner.next(), scanner.next(), scanner.nextDouble());
} else {
throw new InputMismatchException("Invalid format.");
}
}
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(NetworkViewer.this, "Could not open the file", "Error", JOptionPane.ERROR_MESSAGE);
} catch (InputMismatchException mse) {
JOptionPane.showMessageDialog(NetworkViewer.this, "Invalid file format", "Error", JOptionPane.ERROR_MESSAGE);
}
}
networkPanel.repaint();
}
});
Aucun commentaire:
Enregistrer un commentaire