7

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?

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Darth Plagueis
  • 910
  • 3
  • 21
  • 39

1 Answers1

5

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.

Darth Plagueis
  • 910
  • 3
  • 21
  • 39
  • and what would be the data type of the variable g in your example code? (I used SparseGraph and the runtime error is that the SparseGraph cannot be cast to edu.uci.ics.jung.graph.Forest) – Bikash Gyawali Jan 23 '13 at 11:41
  • @bikashg It should be something that implements the [Forest](http://jung.sourceforge.net/doc/api/edu/uci/ics/jung/graph/Forest.html) interface, like these: DelegateForest, DelegateTree, OrderedKAryTree (from the documentation), In my case I had my own implementation of Forest interface. – Darth Plagueis Jan 23 '13 at 23:28