import javax.media.j3d.*;
import javax.vecmath.*;

import java.awt.*;

import com.sun.j3d.utils.geometry.*;

public class Tree extends BranchGroup
{
	private double height;
	private double radius;


	public Tree(double _height, double _radius)
	{
		height = _height;
		radius = _radius;

		createTrunk();
		createBranches();
	}


	private void createTrunk()
	{
		Appearance app = new Appearance();

		ColoringAttributes ca = new ColoringAttributes();
		ca.setColor(new Color3f(new Color(128, 64, 0)));
		app.setColoringAttributes(ca);

		float trunkRadius = (float) (radius/4);
		float trunkHeight = (float) (height/3);

		Cylinder trunk = new Cylinder(trunkRadius, trunkHeight, app);

		TransformGroup tg = new TransformGroup();
		Transform3D t3d = new Transform3D();
		t3d.set(new Vector3d(0, trunkHeight/2, 0));
		tg.setTransform(t3d);

		tg.addChild(trunk);

		addChild(tg);
	}


	private void createBranches()
	{
		Appearance app = new Appearance();

		ColoringAttributes ca = new ColoringAttributes();
		ca.setColor(new Color3f(new Color(0, 128, 0)));
		app.setColoringAttributes(ca);

		float branchRadius = (float) (radius);
		float branchHeight = (float) (height * 2.0 / 3.0);
		float trunkHeight = (float) (height/3);

		Cone branch = new Cone(branchRadius, branchHeight);

		branch.setAppearance(app);

		TransformGroup tg = new TransformGroup();
		Transform3D t3d = new Transform3D();
		t3d.set(new Vector3d(0, branchHeight/2 + trunkHeight, 0));
		tg.setTransform(t3d);

		tg.addChild(branch);

		addChild(tg);
	}
}
