package ciips.animation.tree; import java.awt.*; import ciips.animation.*; /** * This class regards a TreeNode as a binary tree. * Its draw method draws the descending arcs also. * @see TreeNode * @see DrawingPanel#addDrawingObj * @see DrawingPanel#removeObj */ public class BinaryTree extends TreeNode implements DrawingObj { /** * Create a new tree */ public BinaryTree() { } // Constructor 1 /** Start at a node and set the positions for the sub-tree elements **/ public void setPosition( int x, int y, int dx, int dy ) { this.x = x; this.y = y; int dx2 = dx/2; if( left != null ) { left.setPosition( x-dx2, y+dy, dx2, dy ); } if( right != null ) { right.setPosition( x+dx2, y+dy, dx2, dy ); } } /** * Sets the depth of this node corresponding to the root node of the tree. * @param depth Depth of the node. */ public void setDepth(int depth) { this.depth = depth; } /** * Move the node and all its branches based on the parameters. * @param x The horizontal destination position of this node. * @param y The vertical destination position of this node. */ public void move(int x, int y) { int dx = x - this.x; int dy = y - this.y; moveTreeNode(dx, dy); } /** * Move the tree starting with node dx pixels to the right and dy * pixels down. * @param node The root node of the tree to be moved. * @param dx The change in x direction. * @param dy The change in y direction. */ public void moveTreeNode(int dx, int dy) { x += dx; y += dy; if (!isLeaf()) { if (getLeftTreeNode() != null) getLeftTreeNode().moveTreeNode(dx, dy); if (getRightTreeNode() != null) getRightTreeNode().moveTreeNode(dx, dy); } } /** Draw a node and its subtree */ public void drawSubTree( Graphics g ) { draw( g ) ; if ( left != null ) left.drawSubTree( g ); if ( right != null ) right.drawSubTree( g ); } public static void drawEdge( Graphics g, TreeNode parent, TreeNode child, boolean highlight ) { g.setColor( Color.black ); g.drawLine(parent.x + 6, parent.y + 30, child.x + 10, child.y); if (highlight) { g.setColor( Color.blue ); g.drawLine(parent.x + 5, parent.y + 30, child.x + 9, child.y); g.drawLine(parent.x + 4, parent.y + 30, child.x + 8, child.y); } } /** * This method draws the node on the corresponding graphical context * normally passed in from the drawing panel. */ public void draw(Graphics g) { System.out.println("TreeNode:draw " + getWeight() ); if (highlight) g.setColor(Color.black); else g.setColor( nodeColor ); g.fillRect(x, y, 20, 30); g.setColor(Color.black); g.drawRect(x, y, 20, 30); if ( label != null ) { if (getLabel().length() > 0) { g.setColor( labelColor ); g.setFont( hugeFont ); g.drawString(getLabel(), x + 5, y + 12); } } g.setColor( Color.white ); g.setFont( bigFont ); g.drawString(""+getWeight(), x + 2, y+27); // draw links to children if (getLeftTreeNode() != null) { drawEdge( g, this, left, highlightLeft ); } if (getRightTreeNode() != null) { drawEdge( g, this, right, highlightRight ); } System.out.println("BinaryTree:draw " + getWeight() + " exit" ); } } // class BinaryTree