Learning JUNG – Java Universal Network/Graph Framework

I’m searching for “user-friendly” java graph frameworks for an application that I’m developing for my studies. I stumbled upon JUNG. After 15 minutes searching and reading, I managed to create a directed graph and show it on screen.

package com.vainolo;

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 JungLearning {
  public static void main(String[] args) {
    DirectedSparseGraph g = new DirectedSparseGraph();
    g.addVertex("Vertex1");
    g.addVertex("Vertex2");
    g.addVertex("Vertex3");
    g.addEdge("Edge1", "Vertex1", "Vertex2");
    g.addEdge("Edge2", "Vertex1", "Vertex3");
    g.addEdge("Edge3", "Vertex3", "Vertex1");
    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);
  }
}

And this is the output graph:

Next step: Try to show labels and try to change the vertex shapes.

No related posts.

This entry was posted in Programming and tagged , , , . Bookmark the permalink.
  • Lucas

    Hi, i have an exception while i am running your program :

    Exception in thread “main” java.lang.NoSuchMethodError: edu.uci.ics.jung.graph.AbstractGraph.getDefaultEdgeType()Ledu/uci/ics/jung/graph/util/EdgeType;
    at edu.uci.ics.jung.graph.AbstractGraph.addEdge(AbstractGraph.java:55)
    at jungtest5.JungLearning.main(JungLearning.java:20)
    Java Result: 1

    Of course, all libraries are imported. Do you know what should i do to fix it ?

    • admin

      Hi Lucas. Just ran the program again and all seems to work correctly. Did you do any changes to the file?

  • Johny

    Hello, Do you know how can we make this graph editable, like i see in their GraphEditDemo example, but it sounds too hard to be understandable for me. It would be nice if you could give me some idea how to make it editable so that you can freely align the nodes on the layout with mouse cursor, thanks