Class TreeDrawing
- All Implemented Interfaces:
Serializable
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 Node
s. Drawings of
BinaryTree
s 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
method. This method
returns a new getDrawing()
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final int
The width in characters of the horizontal separation between two nodes.static final char
The character used as the standard for determining the character width.static final int
The height in lines of text of the vertical separation between parents and children. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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
Modifier and TypeMethodDescriptionReturns aTreeComponent
displaying this drawing usingTreeComponent.DEFAULT_FONT
.getDrawing
(Font fnt) Returns aTreeComponent
displaying this drawing using the given Font.int
Returns the height of this tree in text lines.int
getWidth()
Returns the width of this tree in characters.void
Paints the TreeDrawing on the given graphics context.
-
Field Details
-
HORIZONTAL_SEPARATION
public static final int HORIZONTAL_SEPARATIONThe width in characters of the horizontal separation between two nodes.- See Also:
-
VERTICAL_SEPARATION
public static final int VERTICAL_SEPARATIONThe height in lines of text of the vertical separation between parents and children.- See Also:
-
STANDARD_CHARACTER
public static final char STANDARD_CHARACTERThe 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
Constructs a drawing of a tree with the given root and children.- Parameters:
root
- a drawing of the root without borderschildren
- drawings of each of the children- Throws:
NullPointerException
- if either argument isnull
-
TreeDrawing
Constructs a drawing of the given tree. The foreground of the drawing is black. The contents of nodes are obtained using theString.valueOf(Object)
method.- Parameters:
tree
- The tree to be drawnmaxHeight
- The maximum height to display. If this value is negative, no nodes will be displayed.- See Also:
-
TreeDrawing
Constructs a colorized drawing of the given tree. The contents of nodes are obtained using theString.valueOf(Object)
method.- Parameters:
tree
- The tree to be drawn. Ifnull
, 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. Ifnull
, each node is colored black.- Throws:
RuntimeException
- ifcol
throws a RuntimeException on any node oftree
- 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
Returns aTreeComponent
displaying this drawing usingTreeComponent.DEFAULT_FONT
.- Returns:
- a component containing a drawing using the default font.
-
getDrawing
Returns aTreeComponent
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
- iffnt
isnull
-
paint
Paints the TreeDrawing on the given graphics context.- Parameters:
g
- the graphics context on which to paint
-