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.
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 ?
Hi Lucas. Just ran the program again and all seems to work correctly. Did you do any changes to the file?
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
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
It’s been a long time since I worked on this.
That saved me some time. Thank you very much 🙂
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)
SOLUTION ;
please add : jung-api-2.0.jar :
http://java2s.com/Code/Jar/j/Downloadjungapi20jar.htm
Thanks a lot!
Hi, I have encountered this error
Exception in thread “main” java.lang.ArrayStoreException: edu.uci.ics.jung.algorithms.layout.CircleLayout
could you help please ?
Sorry Ahmed but it’s been quite a long time since I wrote this and I don’t have an environment to check. Try stackoverflow