Vainolo's Blog

Learning JUNG – Java Universal Network/Graph Framework

9 comments

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:

You can fetch this example’s code here

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

Written by vainolo

February 14th, 2011 at 12:27 am

Posted in Programming

Tagged with , , ,

9 Responses to 'Learning JUNG – Java Universal Network/Graph Framework'

Subscribe to comments with RSS or TrackBack to 'Learning JUNG – Java Universal Network/Graph Framework'.

  1. 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 ?

    Lucas

    3 Jun 11 at 16:24

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

    admin

    9 Jun 11 at 10:44

  3. 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

    Johny

    18 Jan 12 at 17:10

  4. Hello! admin I met exception when running the program probably due to lack of library: import edu.uci.ics.jung.algorithms.layout.CircleLayout.bao admin error at edu.mong only help

    Luc Hoang Van

    10 Jan 14 at 14:05

  5. It’s been a long time since I worked on this.

    vainolo

    12 Jan 14 at 09:54

  6. That saved me some time. Thank you very much 🙂

    Rafael de Paula Pinto Paravia

    25 Aug 15 at 23:46

  7. Haven anybody solution for this Problem: :(:(

    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 probegrafisch.JungLearning.main(JungLearning.java:20)

    Fire

    24 Sep 15 at 14:51

  8. SOLUTION ;

    please add : jung-api-2.0.jar :

    http://java2s.com/Code/Jar/j/Downloadjungapi20jar.htm

    Fire

    24 Sep 15 at 17:18

  9. Thanks a lot!

    Anna

    16 Oct 17 at 12:27

Leave a Reply

%d bloggers like this: