Class TreeDrawing

java.lang.Object
edu.ksu.cis.viewer.TreeDrawing
All Implemented Interfaces:
Serializable

public final class TreeDrawing extends Object implements Serializable
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 (rhowell@ksu.edu)
See Also:
  • Field Details

    • HORIZONTAL_SEPARATION

      public static final int HORIZONTAL_SEPARATION
      The width in characters of the horizontal separation between two nodes.
      See Also:
    • VERTICAL_SEPARATION

      public static final int VERTICAL_SEPARATION
      The height in lines of text of the vertical separation between parents and children.
      See Also:
    • STANDARD_CHARACTER

      public static final char STANDARD_CHARACTER
      The character used as the standard for determining the character width.
      See Also:
  • Constructor Details

    • 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:
    • 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:
  • Method Details

    • getWidth

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

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

      public TreeComponent getDrawing()
      Returns a TreeComponent displaying this drawing using TreeComponent.DEFAULT_FONT.
      Returns:
      a component containing a drawing using the default font.
    • getDrawing

      public TreeComponent getDrawing(Font fnt) throws NullPointerException
      Returns a TreeComponent displaying this drawing using the given Font.
      Parameters:
      fnt - the font to use
      Returns:
      a component containing a 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.
      Parameters:
      g - the graphics context on which to paint