所以我實際上有2個問題。 首先,我可以指定頂點的顯示順序嗎?通過這個我的意思是,當我嘗試直觀地看到頂點的直線時,有時會首先到最後,有時最後到第一個。這是我的代碼片段。還有什麼更好的方法來做直線比改變垂直維度低?JUNG頂點,二叉樹的JUNG的靜態順序
Tree<GraphNode, String> g1 = q.transformToGraph();
Layout<GraphNode, String> layout = new ISOMLayout<GraphNode, String>(g1);
layout.setSize(new Dimension(1600, 25));
VisualizationViewer<GraphNode, String> vv = new VisualizationViewer<GraphNode, String>(layout);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
VertexLabelAsShapeRenderer<GraphNode, String> vlasr = new VertexLabelAsShapeRenderer<GraphNode, String>(vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelTransformer(
new ChainedTransformer<GraphNode, String>(
new Transformer[]{new MyLabeller()}));
vv.getRenderer().setVertexLabelRenderer(vlasr);
return vv;
transformToGraph函數返回DirectedSparseGraph。
也有沒有什麼辦法沒有寫我自己的佈局,使一個很好的二叉樹可視化。特別是BST,因爲TreeLayout使得邊緣在一些邊緣的長度和掉期交易雙方不同(左是右和周圍的其他方法)
Tree<GraphNode, String> g1 = (DelegateTree) q.transformToGraph();
Layout<GraphNode, String> layout = new DynamicTreeLayout<GraphNode, String>(g1, 100, 100);
layout.setSize(new Dimension(1600, 100)); VisualizationViewer<GraphNode, String> vv = new VisualizationViewer<GraphNode, String>(layout);
vv.getRenderContext().setEdgeShapeTransformer(new EdgeShape.Line());
VertexLabelAsShapeRenderer<GraphNode, String> vlasr = new VertexLabelAsShapeRenderer<GraphNode, String>(vv.getRenderContext());
vv.getRenderContext().setVertexShapeTransformer(vlasr);
vv.getRenderContext().setVertexLabelTransformer(
new ChainedTransformer<GraphNode, String>(
new Transformer[]{new MyLabeller()}));
vv.getRenderer().setVertexLabelRenderer(vlasr);
return vv;
這裏transformToGraph改變了我對BST DelegateTree。
下面是如何
@Override
public DelegateTree<GraphNode, String> transformToGraph() {
DelegateTree<GraphNode, String> graph = new DelegateTree<GraphNode, String>();
if (root == null) {
graph.setRoot(null);
return graph;
}
GraphNode rootNode = new GraphNode(root.getValue());
graph.setRoot(rootNode);
if (root.getLeft() == root.getRight()) {
return graph;
}
addNodeToGraph(rootNode, root.getLeft(), graph);
addNodeToGraph(rootNode, root.getRight(), graph);
return graph;
}
protected void addNodeToGraph(GraphNode parent, TreeNode node, DelegateTree<GraphNode, String> graph) {
if (node == null) {
return;
}
GraphNode curr = new GraphNode(node.getValue());
graph.addChild(parent.getValue() + node.getValue(), parent, curr);
addNodeToGraph(curr, node.getLeft(), graph);
addNodeToGraph(curr, node.getRight(), graph);
}
TreeLayout應該使用一致的x和y間距來渲染邊緣;你有截圖顯示,否則? –