如何使用JUNG绘制树形层次结构?

时间:2010-04-08 19:41:41

标签: java layout tree graph jung

我是JUNG的新手。我尝试使用TreeLayout绘制树的图形,但树永远不会像真正的树一样出现。每次树看起来都不一样。如何使树看起来像一棵普通的树,根部位于顶部&其余的节点从它下降?

1 个答案:

答案 0 :(得分:5)

在将Vertexes添加到图表后,您必须初始化TreeLayout,我试过了,它对我有效。

您必须执行以下操作:(请注意,这是一个1年前的代码,您可能会发现它有点过时了)

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是表示图表中顶点的类,GraphEdge表示图表中的边。