Visualize graphs in java

1.5k Views Asked by At

I'm currently working on a university project to implement simple graph algorithms in java (https://en.wikipedia.org/wiki/Graph_(discrete_mathematics)) and I'm struggling to find a simple solution to export my created graphs into a image file (i.e. .png)

I already looked at tools like GraphViz and GraphStream, but they seem to focus more on visualizing their own code to create graphs.

Has anybody knowledge of a simple library or maybe even external tool which can interpret my generated output in a image file? If possible something lightweight and easy to implement.

[My code] ---> [Output] ---> [What I'm in search of] ---> [Image File]

2

There are 2 best solutions below

0
On BEST ANSWER

If you generate output in the Graphviz Dot language, you can visualize it with dotty and export to PNG or other image formats with the dot program. The Dot language is very simple, for example here's a directed graph with 3 nodes making a cycle, and a self-edge in the third node.

digraph {
A -> B
B -> C
C -> A
C -> C
}

This is how it's rendered in dotty:

enter image description here

0
On

Your requirements are loosely stated, but here are some answers:

  • If you want 'in search of' to place both nodes & edges, then the answer above works
  • If you want to place the nodes yourself, graphviz supports this - search the FAQ for "already have all the coordinates for the nodes". dot -Tdot yourinputfile will give an example of the output you need to create. You can skip the pos on the edges.
  • If you want to place both nodes and edges, see above but include the pos on the edges and use neato -n2 (edges are always splines, but straight edges are easy to calculate)
    • Doesn't java have graphice routines you could use?
  • If you want to position nodes and edges but don't like the graphviz solution, try dpic (https://ece.uwaterloo.ca/~aplevich/dpic/) It is slightly lower-level and gives you more control

Good luck