Creating and visualizing graphs - JGraphT
by, 11-19-2011 at 06:54 PM (6257 Views)
Graphs are used to represent knowledge in a visual form. There are many types and forms of graphs. Java provides support for graphical structures. In this post, I will talk about JGraphT which is a free Java graph library.
You may download the library from:
Creating and visualizing the graph is very easy:
We will use ListenableGraph in our example. ListenableGraph is graph that supports listeners on structural change events. So its flexible. I will start with simple example, and then will introduce other tricky things.Java Code:// create a JGraphT graph ListenableGraph g = new ListenableDirectedGraph( DefaultEdge.class ); // create a visualization using JGraph, via the adapter JGraph jgraph = new JGraph( new JGraphModelAdapter( g ) );
I just created a ListenableGraph g and added vertexes and edges between those vertexes. So graph is created but no visual effects are added yet.Java Code:ListenableGraph g = new ListenableDirectedGraph( DefaultEdge.class ); // create a visualization using JGraph, via an adapter m_jgAdapter = new JGraphModelAdapter( g ); JGraph jgraph = new JGraph( m_jgAdapter ); adjustDisplaySettings( jgraph ); getContentPane( ).add( jgraph ); resize( DEFAULT_SIZE ); // add some sample data (graph manipulated via JGraphT) g.addVertex( "v1" ); g.addVertex( "v2" ); g.addVertex( "v3" ); g.addVertex( "v4" ); g.addEdge( "v1", "v2" ); g.addEdge( "v2", "v3" ); g.addEdge( "v3", "v1" ); g.addEdge( "v4", "v3" );