如何画一棵树再presenting连接节点图?

编程入门 行业动态 更新时间:2024-10-14 10:43:01
本文介绍了如何画一棵树再presenting连接节点图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我要在Java GUI中显示一棵树,但我不知道怎么办。再$ P $树psents连接的节点图,像这样的:

I want to display a tree in a Java GUI, but I dont know how. 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?

推荐答案

我能想到的最简单的方法是写一个类,扩展了的JP​​anel 并覆盖其的paintComponent()方法。在paint方法可以通过迭代树和油漆每个节点。下面是一个简单的例子:

The simplest way I can think of is to write a class that extends JPanel and override its paintComponent() method. In the paint method you can iterate through the tree and paint each node. 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.

更多推荐

如何画一棵树再presenting连接节点图?

本文发布于:2023-11-29 21:03:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647619.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   棵树   画一   presenting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!