import java.awt.Graphics;
import java.util.Random;
import javax.swing.JApplet;

public class Tree extends JApplet
{
	Random rand;

	private void draw(Graphics g, int x, int y, int dx, int dy, int depth )
	{
		int tmpx, tmpy;
		double pi = Math.PI;

		/*
		if( rand.nextDouble() > (4.0 / depth) ) {
			return;
		}
		*/
		if( depth > 7 ) {
			return;
		}

		g.drawLine( x, y, x + dx, y + dy );

		x += dx; y += dy;
		dx *= 0.6; dy *= 0.6;
		depth++;

		for( double d = -pi / 4; d <= pi / 4; d += pi / 2)
		{
			tmpx = (int)(dx * Math.cos( d ) - dy * Math.sin( d ));
			tmpy = (int)(dx * Math.sin( d ) + dy * Math.cos( d ));
			draw( g, x, y, tmpx, tmpy, depth );
		}
	}

	public void paint(Graphics g)
	{
		super.paint(g);
		draw( g, 200, 300, 0, -100, 1 );
	}

	public void init()
	{
		/*
		rand = new Random();
		*/
	}
}
