Search code examples
javalayouttreegraphjung

how can i draw a tree hierarchy using JUNG?


I'm new to JUNG. I tried to draw a graph of a tree using the TreeLayout but the tree never comes out like a real tree. Every time the tree looks different. How can I make the tree look like a normal tree with the root on top & the rest of the nodes descending from it?


Solution

  • You have to Initialize the TreeLayout after adding the Vertexes to the graph, I tried that and it worked for me.

    You have to do something like the following: (please note that this is a 1 year old code that i had, you might find it to be a little out dated)

    Layout<GraphVertex, GraphEdge> layout; //create a layout
    layout = new TreeLayout<GraphVertex, GraphEdge>((Forest<GraphVertex, GraphEdge>) g); 
    // initialize your layout using the graph you created, which has to be of type forest
    vv.setGraphLayout(layout); 
    // set the layout of the visualization viewer you are using to be the layout you just created (the tree layout)
    

    GraphVertex Is the class which represents a vertex in the graph, GraphEdge represents the edges in your graph.