import	java.awt.*;
import	javax.vecmath.*;
import	javax.media.j3d.*;

class TriangleStripArrayDemo extends Frame
{
	public static void main(String[] args)
	{
		new TriangleStripArrayDemo();
	}

	public TriangleStripArrayDemo()
	{
		setTitle("TriangleStripArrayDemo");

		setLayout(new BorderLayout());
		Canvas3D c = new Canvas3D(null);
		add("Center", c);

		UniverseBuilder u = new UniverseBuilder(c);
		BranchGroup scene = createSceneGraph();
		u.addBranchGraph(scene);

		setSize(300,400);
		show();
	}

	public BranchGroup createSceneGraph()
	{
		BranchGroup branchGroup = new BranchGroup();
		TransformGroup transformGroup = new TransformGroup();
		transformGroup.addChild(new Triangles().getShape());
		branchGroup.addChild(transformGroup);
		return branchGroup;
	}
}

class UniverseBuilder extends Object
{
	Locale			locale;

	UniverseBuilder(Canvas3D c)
	{
		Transform3D t = new Transform3D();
		Transform3D t2 = new Transform3D();
		t2.setEuler( new Vector3d(-35.0*(Math.PI/180.0),45.0*(Math.PI/180.0),0.0));
		t.set(4,new Vector3d(4.0,3.0,4.0));
		t.mul(t,t2);

		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 Triangles extends Object
{
	static double verts[] = {
		-1,0,0,
		0,0,0,
		-1,1,0,
		0,1,0,
		0,2,0,

		-1.5,-2,0,
		-0.5,-2,0,
		-1.5,-1,0,
		-0.5,-1,0,
	};

	static float[] colors = {

		1.0f, 0.0f, 0.0f,	
		0.0f, 1.0f, 0.0f,		
		0.0f, 0.0f, 1.0f,		
		1.0f, 1.0f, 0.0f,		
		1.0f, 0.0f, 1.0f,		

		0.0f, 1.0f, 1.0f,		
		1.0f, 0.0f, 0.0f,	
		0.0f, 1.0f, 0.0f,		
		0.0f, 0.0f, 1.0f,		
	};

	static int[] strips = {
		5,4
	};


	private Shape3D shape;

	public Triangles()
	{
		TriangleStripArray triangle = new TriangleStripArray(9, TriangleStripArray.COORDINATES | TriangleStripArray.COLOR_3,strips );

		triangle.setCoordinates(0, verts);
		triangle.setColors(0, colors);
		
		PolygonAttributes polygonAttributes = new PolygonAttributes();
		polygonAttributes.setPolygonMode(PolygonAttributes.POLYGON_LINE);
		Appearance appearance = new Appearance();
		appearance.setPolygonAttributes(polygonAttributes);

		shape = new Shape3D(triangle, appearance);
	}

	public Shape3D getShape()
	{
		return shape;
	}
}

