How to draw a tree representing a graph of connected nodes?

你说的曾经没有我的故事 提交于 2019-11-26 15:27:14

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.

You might consider any of these:

I'd say it's worth to check out Abego's TreeLayout too. 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.

I guess you just need to read about JTree: http://docs.oracle.com/javase/tutorial/uiswing/components/tree.html

And maybe some other general information about Swing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!