import java.awt.*;
import java.awt.event.*;
import java.util.Enumeration;
import javax.vecmath.*;
import javax.media.j3d.*;

class InterpolatorDemo extends Frame
{
	public static void main(String[] args)
	{
		new InterpolatorDemo();
	}

	public InterpolatorDemo()
	{
		setTitle("RotationInterpolatorDemo");
		setLayout(new BorderLayout());
		Canvas3D c = new Canvas3D(null);
		add("Center", c);

		UniverseBuilder u = new UniverseBuilder(c);
		BranchGroup scene = createSceneGraph();
		u.addBranchGraph(scene);

		setSize(300,270);
		show();
	}

	public BranchGroup createSceneGraph()
	{
		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);

		BranchGroup branchGroup = new BranchGroup();
		TransformGroup transformGroup = new TransformGroup(new Transform3D());
		transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

		Transform3D yAxis = new Transform3D();	
		Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
		RotationInterpolator rotationInterpolator = new RotationInterpolator(rotationAlpha, transformGroup, 
						yAxis, 0.0f, (float) Math.PI*2.0f);
		rotationInterpolator.setSchedulingBounds(bounds);

		transformGroup.addChild(new ColorCube().getShape());
		branchGroup.addChild(transformGroup);
		branchGroup.addChild(rotationInterpolator);
		return branchGroup;
	}
}

class UniverseBuilder extends Object
{
	Locale			locale;

	UniverseBuilder(Canvas3D c)
	{
		Transform3D t = new Transform3D();
		t.set(1,new Vector3d(0.0,0.0,5.0));

		VirtualUniverse	universe = new VirtualUniverse();
		locale = new Locale(universe);

		PhysicalBody body = new PhysicalBody();
		PhysicalEnvironment environment = new PhysicalEnvironment();
		BranchGroup viewPlatformBranchGroup = new BranchGroup();
		TransformGroup viewPlatformTransformGroup = new TransformGroup(t);
		ViewPlatform viewPlatform = new ViewPlatform();

		View view = new View();
		view.addCanvas3D(c);
		view.setPhysicalBody(body);
		view.setPhysicalEnvironment(environment);
		view.attachViewPlatform(viewPlatform);
		
		viewPlatformTransformGroup.addChild(viewPlatform);
		viewPlatformBranchGroup.addChild(viewPlatformTransformGroup);
		locale.addBranchGraph(viewPlatformBranchGroup);
	}

	void addBranchGraph(BranchGroup bg)
	{
		locale.addBranchGraph(bg);
	}
}

class ColorCube extends Object
{
	static double verts[] = {
		// elso lap
		1.0, -1.0,  1.0,	1.0,  1.0,  1.0,
		-1.0,  1.0,  1.0,	-1.0, -1.0,  1.0,
		// hatso lap
		-1.0, -1.0, -1.0,	-1.0,  1.0, -1.0,
		1.0,  1.0, -1.0,	1.0, -1.0, -1.0,
		// jobb lap
		1.0, -1.0, -1.0,	1.0,  1.0, -1.0,
		1.0,  1.0,  1.0,	1.0, -1.0,  1.0,
		// bal lap
		-1.0, -1.0,  1.0,	-1.0,  1.0,  1.0,
		-1.0,  1.0, -1.0,	-1.0, -1.0, -1.0,
		// fedo lap
		1.0,  1.0,  1.0,	1.0,  1.0, -1.0,
		-1.0,  1.0, -1.0,	-1.0,  1.0,  1.0,
		// alaplap
		-1.0, -1.0,  1.0,	-1.0, -1.0, -1.0,
		1.0, -1.0, -1.0,	1.0, -1.0,  1.0
	};

	static float[] colors = {
		// elso lap (piros)
		1.0f, 0.0f, 0.0f,		1.0f, 0.0f, 0.0f,
		1.0f, 0.0f, 0.0f,		1.0f, 0.0f, 0.0f,
		// hatso lap (zold)
		0.0f, 1.0f, 0.0f,		0.0f, 1.0f, 0.0f,
		0.0f, 1.0f, 0.0f,		0.0f, 1.0f, 0.0f,
		// jobb lap (kek)
		0.0f, 0.0f, 1.0f,		0.0f, 0.0f, 1.0f,
		0.0f, 0.0f, 1.0f,		0.0f, 0.0f, 1.0f,
		// bal lap (sarga)
		1.0f, 1.0f, 0.0f,		1.0f, 1.0f, 0.0f,
		1.0f, 1.0f, 0.0f,		1.0f, 1.0f, 0.0f,
		// fedo lap (magenta)
		1.0f, 0.0f, 1.0f,		1.0f, 0.0f, 1.0f,
		1.0f, 0.0f, 1.0f,		1.0f, 0.0f, 1.0f,
		// alaplap (cian)
		0.0f, 1.0f, 1.0f,		0.0f, 1.0f, 1.0f,
		0.0f, 1.0f, 1.0f,		0.0f, 1.0f, 1.0f
	};

	private Shape3D shape;

	public ColorCube()
	{
		QuadArray cube = new QuadArray(24, QuadArray.COORDINATES | QuadArray.COLOR_3);

		cube.setCoordinates(0, verts);
		cube.setColors(0, colors);

		shape = new Shape3D(cube, new Appearance());
	}

	public Shape3D getShape()
	{
		return shape;
	}
}
