我的代码像这样工作并直接打印图形的所有边缘,但是我想在我的图形流窗口中直接单击添加按钮,并且当我单击一次打印图形的那一行时,而不是再次单击时打印另一行,直到图形可视化结束,正确的 Dijkstra 算法。
我怎么能做到这一点?我如何在 Graph Stream 上添加按钮?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.graphstream.algorithm.Dijkstra;
import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.Path;
import org.graphstream.graph.implementations.SingleGraph;
public class DijkstraExample
{
public static Graph exampleGraph()
{
Graph g = new SingleGraph("example");
g.addNode("A").addAttribute("xy", 0, 1);
g.addNode("B").addAttribute("xy", 1, 2);
g.addNode("C").addAttribute("xy", 1, 1);
g.addNode("D").addAttribute("xy", 1, 0);
g.addNode("E").addAttribute("xy", 2, 2);
g.addNode("F").addAttribute("xy", 2, 1);
g.addEdge("AB", "A", "B").addAttribute("length", 14);
g.addEdge("AC", "A", "C").addAttribute("length", 9);
g.addEdge("AD", "A", "D").addAttribute("length", 7);
g.addEdge("BC", "B", "C").addAttribute("length", 2);
g.addEdge("CD", "C", "D").addAttribute("length", 10);
g.addEdge("BE", "B", "E").addAttribute("length", 9);
g.addEdge("CF", "C", "F").addAttribute("length", 11);
g.addEdge("DF", "D", "F").addAttribute("length", 15);
g.addEdge("EF", "E", "F").addAttribute("length", 6);
for (Node n: g)
n.addAttribute("label", n.getId());
for (Edge e: g.getEachEdge())
e.addAttribute("label", "" + (int) e.getNumber("length"));
return g;
}
public static void main(String[] args)
{
Graph g = exampleGraph();
g.display(false);
Dijkstra dijkstra = new Dijkstra(Dijkstra.Element.EDGE, null, "length");
dijkstra.init(g);
dijkstra.setSource(g.getNode("A"));
dijkstra.compute();
for (Node node: g)
System.out.printf("%s->%s:%10.2f%n", dijkstra.getSource(), node, dijkstra.getPathLength(node));
for (Node node: dijkstra.getPathNodes(g.getNode("B")))
node.addAttribute("ui.style", "fill-color: blue;");
for (Edge edge: dijkstra.getTreeEdges())
edge.addAttribute("ui.style", "fill-color: red;");
System.out.println(dijkstra.getPath(g.getNode("B")));
List < Node > list1 = new ArrayList < Node > ();
for (Node node: dijkstra.getPathNodes(g.getNode("B")))
list1.add(0, node);
List < Node > list2 = dijkstra.getPath(g.getNode("B")).getNodePath();
dijkstra.clear();
dijkstra = new Dijkstra(Dijkstra.Element.NODE, null, null);
dijkstra.init(g);
dijkstra.setSource(g.getNode("A"));
dijkstra.compute();
for (Node node: g)
System.out.printf("%s->%s:%10.2f%n", dijkstra.getSource(), node, dijkstra.getPathLength(node));
Iterator < Path > pathIterator = dijkstra.getAllPathsIterator(g.getNode("F"));
while (pathIterator.hasNext())
System.out.println(pathIterator.next());
}
}