0

My code works like this and directly print all the edges of graph, but I want to add button on click direct at my Graph Stream window, and when I click one time on that one line of the graph is printed, than when I click again another line is printed, and like that until the end of visualization of graph, right Dijkstra algorithm.

How I can do this? How i can add button on 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());
    }
}
Kingsley
  • 14,398
  • 5
  • 31
  • 53
  • 1
    Hi Gorgi! Welcome to Stack Overflow. Please spend 5 minutes of your time to read [How to create a Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). I guarantee that, if you follow this guideline you when you ask questions, you will always get solutions by the other members of the community. Your current question is not one to attract others, as it is bulky, has a big chunk of code and your question is not presented in a clear and precise manner. – Soutzikevich Jan 31 '19 at 17:23
  • I re-indented the code, and fixed some grammar, but I don't completely understand the wording of the question. Perhaps @Gorgi, you could edit the question to be more concise. I sounds like the interface needs to handle a click on a graph line, and then some details are output. I don't understand what the "button" is for. This made it impossible for me to re-word the question on your behalf. – Kingsley Jan 31 '19 at 23:36
  • @Kingsley my idea is when i click on button one edge of the tree is printed, than another click another edge of the tree is printed, that does not matter, if someone know how to add button on Graph Stream it will help me, that is my question "How to add button on click on Graph Stream". thanks. – Gorgi Mitrevski Feb 01 '19 at 18:58

1 Answers1

0

If you add to add a button in your interface, your need to create your own interface. You can look this example : https://github.com/graphstream/gs-ui-swing/blob/master/src-test/org/graphstream/ui/viewer_swing/test/AllSwingTest.java

You have your swing panel, you can do whatever you want.

H.Brahimi
  • 254
  • 1
  • 7