A very common library handle graphs in Java is provided by the JGraphT project: http://jgrapht.org/.

If you have produced a graph in your program and would like to visualize this there are two easy and straight forward ways to get a result:

GraphViz DOT Format

JGraphT provides an exporter for the GraphViz DOT format. To generate this format, simply use the following code:

DOTExporter exporter = new DOTExporter();
String targetDirectory = "testresults/graph/";
new File(targetDirectory).mkdirs();
exporter.export(new FileWriter(targetDirectory + "initial-graph.dot"), graph);

To view a .dot graph file in Eclipse, you can install the GraphViz Viewer provided by the TextUML project:
Their update site includes a separate feature for the GraphViz viewer.

JGraphT / JGraph Integration

JGraphT provides an integration with the JGraph library for graph visualisation (http://jgrapht.org/visualizations.html).

Here you will find some example code how to run a simple visualization of your graph.
Note: This code does not take any layouting into account and simply draws all nodes on top of each other.

import javax.swing.JFrame;

import org.jgraph.JGraph;
import org.jgrapht.ext.JGraphModelAdapter;
import org.junit.Test;
import org.splevo.tests.SPLevoTestUtil;
import org.splevo.vpm.analyzer.graph.VPMGraph;
import org.splevo.vpm.analyzer.graph.VPMRelationshipEdge;
import org.splevo.vpm.variability.VariationPoint;
import org.splevo.vpm.variability.VariationPointModel;

JFrame frame = new JFrame();
frame.setSize(400, 400);
JGraph jgraph = new JGraph(new JGraphModelAdapter(graph));
while (true) {

This code is compatible to the JGraph library shipped with JGraphT only.

Visualizing your JGraphT Graph Instance

Post navigation

Leave a Reply