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

7

我是JUNG的新手。我尝试使用TreeLayout绘制一棵树形图,但是树形图从未像真正的树那样呈现出来。每次树都看起来不同。如何使树看起来像一个正常的树,根在顶部,其余节点从上往下排列?

1个回答

5

在向图形添加顶点后,您必须初始化TreeLayout。我尝试过这样做,对我有用。

您需要执行以下操作:(请注意,这是一年前的代码,可能有些过时)

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 代表图中的边。


在你的示例代码中,变量g的数据类型是什么?(我使用了SparseGraph,运行时错误是SparseGraph无法转换为edu.uci.ics.jung.graph.Forest) - Bikash Gyawali
@bikashg 它应该是实现Forest接口的东西,比如DelegateForest、DelegateTree、OrderedKAryTree(来自文档)。在我的情况下,我有自己的Forest接口实现。 - Darth Plagueis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接