/**
 *  The FancyTree class draws a recursively generated tree that looks 
 *  quasi-realistic.  See the Simple Tree example for a simpler but more 
 *  customizable one.
 */

import java.awt.*;
import javax.swing.*;
import com.otherwise.jurtle.*;

public class FancyTree extends Turtle
{
    private static final int MAX_DEPTH = 8;
    private static final Color BROWN = new Color( 110, 74, 32 );
    private static final int LEAF_DELAY = 1;

    /**
     *  Main entry point to the code.  Sets up the environment and then calls
     *  the drawTree() method.
     */
    public void runTurtle()
    {

        Dimension panelSize = getDisplaySize();
        int trunkSize = panelSize.height / 4;

        setAutoUpdate( false );
        hideTurtle();
        penUp();
        setPosition( panelSize.width / 2, panelSize.height * .9 );
        penDown();

        drawTree( trunkSize, MAX_DEPTH );

    }

    /**
     *  Uses recursion to draw a tree.  First draws a branch and then calls 
     *  drawTree() twice at the end of the branch.
     */
    private void drawTree ( int distance, int depth )
    {
        if ( depth > 0 )
        {
            updateDisplay();
            pause( LEAF_DELAY );

            drawBranch( distance, depth );
            right( 30 );
            drawTree( ( int ) ( distance * .75 ), depth - 1 );
            left( 60 );
            drawTree( ( int ) ( distance * .75 ), depth - 1 );
            right( 30 );
            penUp();
            backward( distance );
            penDown();
        }
    }


    /**
     *  Draws a single branch of the tree.  The color and width of the branch
     *  depends upon how long it is as specified by distance.
     */
    private void drawBranch ( int distance, int depth )
    {
        if ( depth == 1 )
            setPenColor( Color.green );
        else
            setPenColor( BROWN );

        setPenWidth( distance / 10 );
        forward( distance );
    }


}
