Compare commits

...

2 commits

2 changed files with 61 additions and 13 deletions

View file

@ -29,7 +29,6 @@ import java.util.Set;
* @version 2.04 2021/9/11 Test as plugin
*/
public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
private final int UHDTHRESHOLD = 1920;
private final String STANDARDENCODING = "UTF-8";
private JMenuItem connect, exit, open, test, retest, textView, graphicView, clear;
@ -155,8 +154,7 @@ public class ExBoxFrame extends JFrame implements ActionListener, ItemListener {
ex.printStackTrace();
}
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
double scaleFaktor = (screenSize.getWidth() <= UHDTHRESHOLD) ? 1 : 2;
double scaleFaktor = (double)Toolkit.getDefaultToolkit().getScreenResolution() / 96.0;
setFontSize((int) (11 * scaleFaktor));
setSize(new Dimension((int) (400 * scaleFaktor), (int) (400 * scaleFaktor)));
setTitle("ExBox");

View file

@ -2,26 +2,74 @@ package ch.nuth.zhaw.exbox;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
public class RouteServer implements CommandExecutor {
/**
build the graph given a text file with the topology
* build the graph given a text file with the topology
*/
public Graph<DijkstraNode, Edge> createGraph(String topo) throws Exception {
// TODO implement
Graph<DijkstraNode, Edge> graph = new AdjListGraph<>(DijkstraNode.class, Edge.class);
for (String line : topo.split("\r?\n")) {
String[] attributes = line.split(" ");
try {
for (int i = 0; i <= 1; i++) {
graph.addEdge(attributes[i], attributes[1 - i], Double.parseDouble(attributes[2]));
}
} catch (Throwable e) {
throw new Exception(e);
}
}
return graph;
}
/**
apply the dijkstra algorithm
* apply the dijkstra algorithm
*/
public void dijkstraRoute(Graph<DijkstraNode, Edge> graph, String from, String to) {
// TODO implement
Queue<DijkstraNode> pendingNodes = new PriorityQueue<>();
DijkstraNode start = graph.findNode(from);
DijkstraNode end = graph.findNode(to);
start.setDist(0);
pendingNodes.add(start);
while (!pendingNodes.isEmpty()) {
DijkstraNode current = pendingNodes.poll();
current.setMark(true);
if (current == end) {
return;
}
for (Edge edge : current.getEdges()) {
if (edge.getDest() instanceof DijkstraNode node &&
!node.getMark()) {
double dist = current.getDist() + edge.getWeight();
if (!pendingNodes.contains(node) ||
dist < node.getDist()) {
node.setDist(dist);
node.setPrev(current);
if (pendingNodes.contains(node)) {
pendingNodes.remove(node);
}
pendingNodes.add(node);
}
}
}
}
}
/**
find the route in the graph after applied dijkstra
the route should be returned with the start town first
* find the route in the graph after applied dijkstra
* the route should be returned with the start town first
*/
public List<DijkstraNode> getRoute(Graph<DijkstraNode, Edge> graph, String to) {
List<DijkstraNode> route = new LinkedList<>();
@ -39,7 +87,9 @@ public class RouteServer implements CommandExecutor {
List<DijkstraNode> route = getRoute(graph, "Lugano");
// generate result string
StringBuilder builder = new StringBuilder();
for (DijkstraNode rt : route) builder.append(rt).append("\n");
for (DijkstraNode rt : route) {
builder.append(rt).append("\n");
}
return builder.toString();
}
}