JUNG:按顺序放置树节点

时间:2012-04-18 18:34:03

标签: java tree visualization jung

向我的DelegateTree添加节点时,它们不会按照我添加的顺序直观显示。 我一直在寻找解决方案,但还没有找到任何东西。 有没有人可能知道如何改变这个?

提前致谢!

编辑:我的代码

/**
 * Generate a visualization of the decision tree.
 * 
 * @param tree
 *            The decision tree
 * @return A component to be placed inside a JPanel
 */
public static GraphZoomScrollPane generateTree(Tree tree,
        GraphicalUserInterface gui) {

    /* Create a new tree */
    DelegateTree<Node, Edge> graphTree = new DelegateTree<Node, Edge>();

    /* Add all nodes and vertices to the tree */
    graphTree.addVertex(tree.getRoot());
    addChildren(tree.getRoot(), graphTree);

    /* Create the visualization */
    DynamicTreeLayout<Node, Edge> treeLayout = new DynamicTreeLayout<Node, Edge>(graphTree, 100, 100);
    VisualizationViewer<Node, Edge> vv = new VisualizationViewer<Node, Edge>(
            treeLayout);
    vv.setDoubleBuffered(true);
    vv.setBackground(Color.WHITE);
    vv.getRenderContext().setEdgeLabelTransformer(
            new ToStringLabeller<Edge>());
    vv.getRenderContext().setEdgeShapeTransformer(
            new EdgeShape.Line<Node, Edge>());
    vv.getRenderContext().setVertexLabelTransformer(
            new ToStringLabeller<Node>());
    vv.getRenderer().getVertexLabelRenderer()
            .setPosition(Renderer.VertexLabel.Position.S);

    vv.addGraphMouseListener(new ClickNode(gui, vv));

    final DefaultModalGraphMouse<Node, Edge> graphMouse = new DefaultModalGraphMouse<Node, Edge>();

    graphMouse.setMode(ModalGraphMouse.Mode.TRANSFORMING);

    vv.setGraphMouse(graphMouse);

    final GraphZoomScrollPane gzsp = new GraphZoomScrollPane(vv);

    return gzsp;
}

/**
 * Recursively add all nodes and edges to the tree.
 * 
 * @param node
 *            The parent node
 * @param tree
 *            The tree visualization
 */
private static void addChildren(Node node, DelegateTree<Node, Edge> tree) {
    for (int i = 0; i < node.getChildren().size(); i++) {
        tree.addChild(new Edge(node.getChildren().get(i).getEdgeLabel()), node, node
        .getChildren().get(i));
        addChildren(node.getChildren().get(i), tree);
    }
}

1 个答案:

答案 0 :(得分:1)

使用DelegateTree的底层实现,根据它们的自然排序(* Sorted * Graph)或使用插入排序(* Ordered * Graph)对其顶点进行排序。