使用Java在GraphStream上添加按钮

时间:2019-01-31 17:16:15

标签: java button graph dijkstra graphstream

我的代码是这样工作的,并直接打印图形的所有边缘,但是我想在“图形流”窗口的“直接单击”上添加按钮,当我单击一次在图形的那一行上打印时,我再次单击,将打印另一行,并像这样直到图形的可视化结束为止,正确的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());
    }
}

1 个答案:

答案 0 :(得分:0)

如果添加要在界面中添加按钮,则需要创建自己的界面。您可以看以下示例:https://github.com/graphstream/gs-ui-swing/blob/master/src-test/org/graphstream/ui/viewer_swing/test/AllSwingTest.java

您拥有摆动面板,您可以做任何您想做的事。