如何用java语言画一棵树 [英] How to draw a tree in java language

查看:62
本文介绍了如何用java语言画一棵树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Java 中解析 XML 文件,之后我必须像这样使用 Jframe 将其表示为树

I am trying to parse an XML file in java, after I have to represent it as a tree using Jframe like this

推荐答案

树通常是更容易像这样布局的链接结构之一,因为路径通常不会合并"或交叉".

Trees are generally one of the easier linked constructs to lay out like this, because paths generally don't "merge" or "cross".

您可以通过从左到右"遍历树以粗略的表格方式接近它:从根开始,并在区域的左上角绘制其表示.然后,一次遍历其左"分支,在与根相同的列"中依次在较低的行"上绘制这些节点的表示.然后,当您移动到右侧"侧节点时,在同一级别右侧可用的下一个列"中绘制该节点.这将生成树结构的斜坡形图.

You can approach it in a roughly tabular way by traversing the tree "left to right": start at the root, and draw its representation at the top left of the area. Then, traverse its "left" branch one level at a time, drawing those nodes' representations on successively lower "rows", in the same "column" as the root. Then, as you move to the "right"-side nodes, draw that node in the next "column" available to the right on the same level. This will produce a ramp-shaped graph of the tree's structure.

您可以添加一些对每个级别的级别和节点数的预分析,这将允许您通过了解图形所需的最大级别数和数量来将树居中"成粗略的金字塔形状该图的每个级别的节点数.但是,这需要在开始绘制任何内容之前遍历整个图形.

You can add some pre-analysis of the number of levels and nodes at each level, which will allow you to "center" the tree into a rough pyramid shape by knowing the maximum number of levels the graph will require and the number of nodes at each level of that graph. But, that requires traversing the entire graph before you start drawing anything.

至于排列"树的节点,使它们适合最小的区域而没有箭头交叉或重叠,这是一个范围远远超过平均 SO 答案的问题.

As for "arranging" a tree's nodes so they fit in the smallest area without arrows crossing or overlapping, that's a problem with a scope far exceeding the average SO answer.

这篇关于如何用java语言画一棵树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆