Skip to content

Learning JUNG – Java Universal Network/Graph Framework

Last updated on 2014-09-08

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.

Published inProgramming

11 Comments

  1. Lucas 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 admin

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

  2. Johny 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

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

  4. Rafael de Paula Pinto Paravia Rafael de Paula Pinto Paravia

    That saved me some time. Thank you very much 🙂

  5. Fire Fire

    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)

  6. Hi, I have encountered this error

    Exception in thread “main” java.lang.ArrayStoreException: edu.uci.ics.jung.algorithms.layout.CircleLayout

    could you help please ?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Musings of a Strange Loop

Subscribe now to keep reading and get access to the full archive.

Continue reading