Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

swing

awt

swt

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?

like image 829
Mohammad Olfatmiri Avatar asked Apr 12 '12 15:04

Mohammad Olfatmiri


1 Answers

You might consider any of these:

  • JHotDraw, cited here, a meta-library for creating custom graph editors.

  • Prefuse visualization library, illustrated here and here.

  • Batik, which implements SVG rendering.

  • JGraph demo and user manual.

  • GraphStream, illustrated here.

  • JFreeChart XYBubbleRenderer

  • A JTree, suggested here, with a custom TreeIcon.

  • A custom renderer, with x based on a fraction of tree breadth and y based on recursion level.

  • A simple graph editor such as draw.GraphPanel, illustrated here.

like image 98
trashgod Avatar answered Sep 20 '22 09:09

trashgod