Skip to content

Instantly share code, notes, and snippets.

@digizeph
Created August 25, 2015 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digizeph/8e39f09fd11d14bbd3ec to your computer and use it in GitHub Desktop.
Save digizeph/8e39f09fd11d14bbd3ec to your computer and use it in GitHub Desktop.
Simple graph visualization using graphstream library.
package visualization;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.SingleGraph;
import java.util.Iterator;
public class PlainGraph {
public static void main(String args[]) {
new PlainGraph();
}
public PlainGraph() {
Graph graph = new SingleGraph("tutorial 1");
graph.addAttribute("ui.stylesheet", styleSheet);
graph.setAutoCreate(true);
graph.setStrict(false);
graph.display();
/* TODO: read file instead of hard code it. */
graph.addEdge("1", "144.232.3.125", "144.232.3.124");
graph.addEdge("2", "144.232.3.125", "144.232.9.151");
graph.addEdge("3", "144.232.3.125", "144.232.20.221");
graph.addEdge("4", "144.232.3.124", "144.232.1.149");
for (Node node : graph) {
node.addAttribute("ui.label", node.getId());
}
explore(graph.getNode("144.232.3.125"));
}
public void explore(Node source) {
Iterator<? extends Node> k = source.getBreadthFirstIterator();
/* Breadth first showup? */
while (k.hasNext()) {
Node next = k.next();
next.setAttribute("ui.class", "marked");
sleep();
}
}
protected void sleep() {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
protected String styleSheet =
"node {" +
" fill-color: black;" +
"}" +
"node.marked {" +
" fill-color: red;" +
"}";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment