Tuesday 6 May 2014

Displaying graphs in java

Recently I found good java graph framework, it is called Jung. Here is an example of the directed graph:

 
package com.test;
 
import java.awt.Dimension;
import javax.swing.JFrame;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import edu.uci.ics.jung.visualization.VisualizationImageServer;
 
public class DirectedGraph{
  public static void main(String[] args) {
    DirectedSparseGraph g = new DirectedSparseGraph();
    g.addVertex("Vert1");
    g.addVertex("Vert2");
    g.addVertex("Vert3");
    g.addEdge("Edge1", "Vert1", "Vert2");
    g.addEdge("Edge2", "Vert1", "Vert3");
    g.addEdge("Edge3", "Vert3", "Vert1");
    VisualizationImageServer vs =
      new VisualizationImageServer(
        new CircleLayout(g), new Dimension(200, 200));
 
    JFrame frame = new JFrame();
    frame.getContentPane().add(vs);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }
}


References:
Jung Tutorial

No comments:

Post a Comment