Search code examples
javagraphjava-2dgraph-algorithm

how to organize graph nodes to draw using java 2d


I created a program that builds and draw nested cyclic graphs (with undirected edges) in a JFrame using java awt.

The problem is that if the position of the nodes is not explicitly specified, or created at random, the graph becomes very messy, with edges crossing and vertex colliding.

I would like to implement an algorithm for repositioning to better distribute the nodes, in a more homogeneous and clean way.

Can someone help me?


Solution

  • You may want to look at GraphViz, which is a program to do exactly this (render graphs). It has a Java API, so you could integrate it into your program, and it's open-source, so if you'd rather implement a solution yourself, perhaps their code could provide some inspiration (just remember to check the license!)

    In addition, their website has a list of resources pertaining to graph drawing theory, which sounds exactly like what you need.