How to draw a tree representing a graph of connected nodes?
Asked Answered
E

4

13

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:

image

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?

Eduction answered 12/4, 2012 at 15:23 Comment(0)
W
8

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.

Wellpreserved answered 12/4, 2012 at 17:58 Comment(1)
I've done that many times, you just need to draw recursively and every time divide the drawing space by the number of child nodes. There are some caveats like if the node representation becomes bigger than the drawing space, but in OP's case they're just numbers so it seems ok up to a certain extentReparable
A
16

You might consider any of these:

Aeroballistics answered 12/4, 2012 at 18:59 Comment(0)
W
8

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.

Wellpreserved answered 12/4, 2012 at 17:58 Comment(1)
I've done that many times, you just need to draw recursively and every time divide the drawing space by the number of child nodes. There are some caveats like if the node representation becomes bigger than the drawing space, but in OP's case they're just numbers so it seems ok up to a certain extentReparable
C
4

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.

Catharinecatharsis answered 14/8, 2013 at 9:34 Comment(0)
I
1

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

Iyeyasu answered 12/4, 2012 at 15:27 Comment(2)
sry but i want to show the tree some thing like this link:lcm.csa.iisc.ernet.in/dsa/img151.gifEduction
Not sure if there are free libraries to build such visual trees. You can always draw it by yourself though with basic Graphics tools.Iyeyasu

© 2022 - 2024 — McMap. All rights reserved.