edu.ksu.cis.viewer
Class TreeDrawing

java.lang.Object
  extended byedu.ksu.cis.viewer.TreeDrawing

public final class TreeDrawing
extends Object

An immutable high-level representation of a drawing of a tree. This representation contains a font-independent description of the size of the drawing along with all the information necessary to draw the tree on a given graphics context. The paint method is used to draw the tree.

There are two general ways of constructing a TreeDrawing. The first is to recursively build it from Nodes. Drawings of BinaryTrees are build in this way. The second way is to build it from an implementation of TreeInterface and optionally an implementation of Colorizer. When this way is used, no attempt is made to detect cycles or overlapping subtrees; instead, a parameter to the constructor specifies the maximum height of the tree drawn.

A TreeComponent containing a graphical representation can be efficiently obtained from the getDrawing() method. This method returns a new TreeComponent each time it is called, so they can be used in multiple containers. These components will all share the same TreeDrawing.

Empty trees are not drawn, but if a node contains both empty and nonempty children, the horizontal padding that would surround a node is included for empty children. Consequently, in a binary tree, the line drawn from a node to its left child always angles to the left, and the line drawn from a node to its right child always angles to the right, even when the other child is empty. On the other hand, it may be more difficult to tell which children may be empty in trees with other branching factors.

Author:
Rod Howell (howell@cis.ksu.edu)
See Also:
BinaryTree, TreeInterface, Colorizer

Field Summary
static int HORIZONTAL_SEPARATION
          The width in characters of the horizontal separation between two nodes.
static char STANDARD_CHARACTER
          The character used as the standard for determining the character width.
static int VERTICAL_SEPARATION
          The height in lines of text of the vertical separation between parents and children.
 
Constructor Summary
TreeDrawing()
          Constructs a drawing of an empty tree.
TreeDrawing(Node root, TreeDrawing[] children)
          Constructs a drawing of a tree with the given root and children.
TreeDrawing(TreeInterface tree, int maxHeight)
          Constructs a drawing of the given tree.
TreeDrawing(TreeInterface tree, int maxHeight, Colorizer col)
          Constructs a colorized drawing of the given tree.
 
Method Summary
 TreeComponent getDrawing()
          Returns a TreeComponent displaying this drawing using TreeComponent.DEFAULT_FONT.
 TreeComponent getDrawing(Font fnt)
          Returns a TreeComponent displaying this drawing using the given Font.
 int getHeight()
          Returns the height of this tree in text lines.
 int getWidth()
          Returns the width of this tree in characters.
 void paint(Graphics g)
          Paints the TreeDrawing on the given graphics context.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

HORIZONTAL_SEPARATION

public static final int HORIZONTAL_SEPARATION
The width in characters of the horizontal separation between two nodes.

See Also:
Constant Field Values

VERTICAL_SEPARATION

public static final int VERTICAL_SEPARATION
The height in lines of text of the vertical separation between parents and children.

See Also:
Constant Field Values

STANDARD_CHARACTER

public static final char STANDARD_CHARACTER
The character used as the standard for determining the character width.

See Also:
Constant Field Values
Constructor Detail

TreeDrawing

public TreeDrawing()
Constructs a drawing of an empty tree.


TreeDrawing

public TreeDrawing(Node root,
                   TreeDrawing[] children)
Constructs a drawing of a tree with the given root and children.

Parameters:
root - a drawing of the root without borders
children - drawings of each of the children
Throws:
NullPointerException - if either argument is null

TreeDrawing

public TreeDrawing(TreeInterface tree,
                   int maxHeight)
Constructs a drawing of the given tree. The foreground of the drawing is black. The contents of nodes are obtained using the String.valueOf(Object) method.

Parameters:
tree - The tree to be drawn
maxHeight - The maximum height to display. If this value is negative, no nodes will be displayed.
See Also:
String.valueOf(Object)

TreeDrawing

public TreeDrawing(TreeInterface tree,
                   int maxHeight,
                   Colorizer col)
Constructs a colorized drawing of the given tree. The contents of nodes are obtained using the String.valueOf(Object) method.

Parameters:
tree - The tree to be drawn. If null, no tree is drawn.
maxHeight - The maximum height to display. If this value is negative, no nodes will be displayed.
col - The Colorizer used to obtain the color of each node. If null, each node is colored black.
Throws:
RuntimeException - if col throws a RuntimeException on any node of tree
See Also:
String.valueOf(Object)
Method Detail

getWidth

public int getWidth()
Returns the width of this tree in characters.


getHeight

public int getHeight()
Returns the height of this tree in text lines.


getDrawing

public TreeComponent getDrawing()
Returns a TreeComponent displaying this drawing using TreeComponent.DEFAULT_FONT.


getDrawing

public TreeComponent getDrawing(Font fnt)
                         throws NullPointerException
Returns a TreeComponent displaying this drawing using the given Font.

Throws:
NullPointerException - if fnt is null

paint

public void paint(Graphics g)
Paints the TreeDrawing on the given graphics context.