[英]How to draw a tree representing a graph of connected nodes?
I want to display a tree in a Java GUI, but I dont know how.我想在 Java GUI 中显示一棵树,但我不知道如何。 The tree represents a graph of connected nodes, like this:
该树表示连接节点的图形,如下所示:
I should say that I have my own tree class:我应该说我有自己的树类:
public class BinaryTree
{
private BinaryNode root;
public BinaryTree( )
{
root = null;
}
public BinaryTree( Object rootItem )
{
root = new BinaryNode( rootItem, null, null );
}
public BinaryTree( Object rootItem,BinaryNode a,BinaryNode b )
{
root = new BinaryNode( rootItem, a, b );
}
public int leavesCount(){
return BinaryNode.leavesCount(root);
}
public boolean equal(BinaryTree a,BinaryTree b){
return BinaryNode.equal(a.root, b.root);
}
public void printPreOrder( )
{
if( root != null )
root.printPreOrder( );
}
public void printInOrder( )
{
if( root != null )
root.printInOrder( );
}
public void printPostOrder( )
{
if( root != null )
root.printPostOrder( );
}
public void makeEmpty( )
{
root = null;
}
public boolean isEmpty( )
{
return root == null;
}
public void merge( Object rootItem, BinaryTree t1, BinaryTree t2 ) throws MergeAbrot
{
if( t1.root == t2.root && t1.root != null )
{
throw new MergeAbrot("MergeAbrot");
}
root=new BinaryNode( rootItem, t1.root, t2.root );
if( this != t1 )
t1.root = null;
if( this != t2 )
t2.root = null;
}
public int size( )
{
return BinaryNode.size( root );
}
public int height( )
{
return BinaryNode.height( root );
}
}
I only want to draw the tree.我只想画树。 How should I do?
我应该怎么做?
You might consider any of these:您可以考虑以下任何一项:
JHotDraw , cited here , a meta-library for creating custom graph editors. JHotDraw ,这里引用,一个用于创建自定义图形编辑器的元库。
Prefuse visualization library , illustrated here and here . Prefuse可视化库,在这里和这里说明。
GraphStream , illustrated here . GraphStream ,此处说明。
A JTree
, suggested here , with a custom TreeIcon
.此处建议的
JTree
,带有自定义TreeIcon
。
A custom renderer , with x based on a fraction of tree breadth and y based on recursion level.自定义渲染器, x基于树宽度的一小部分, y基于递归级别。
A simple graph editor such as draw.GraphPanel
, illustrated here .一个简单的图形编辑器,例如
draw.GraphPanel
,在此处说明。
The simplest way I can think of is to write a class that extends JPanel
and override its paintComponent()
method.我能想到的最简单的方法是编写一个扩展
JPanel
并覆盖其paintComponent()
方法的类。 In the paint method you can iterate through the tree and paint each node.在paint方法中,您可以遍历树并绘制每个节点。 Here is a short example:
这是一个简短的例子:
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JPanelTest extends JPanel {
@Override
public void paintComponent(Graphics g) {
// Draw Tree Here
g.drawOval(5, 5, 25, 25);
}
public static void main(String[] args) {
JFrame jFrame = new JFrame();
jFrame.add(new JPanelTest());
jFrame.setSize(500, 500);
jFrame.setVisible(true);
}
}
Take a stab at painting the tree, if you can't figure it out post what you've tried in your question.尝试绘制树,如果您无法弄清楚,请发布您在问题中尝试过的内容。
I'd say it's worth to check out Abego's TreeLayout too.我会说也值得查看Abego 的 TreeLayout 。 It's essentially a tree layout algorithm so it can be used with any drawing mechanism, but it also contains some demos/examples of drawing graphs in SVG and Swing.
它本质上是一种树布局算法,因此可以与任何绘图机制一起使用,但它还包含一些在 SVG 和 Swing 中绘制图形的演示/示例。
I guess you just need to read about JTree: http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html我想你只需要阅读 JTree: http : //docs.oracle.com/javase/tutorial/uiswing/components/tree.html
And maybe some other general information about Swing也许还有其他一些关于 Swing 的一般信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.