import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.io.*;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.geometry.*;

public class OrdGroupTest extends Applet {

    private SimpleUniverse u;
    private BoundingSphere bounds
        = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);


    public BranchGroup createSceneGraph() {
      // Create the root of the branch graph
      BranchGroup  objRoot  = new BranchGroup();
      OrderedGroup ordGroup = new OrderedGroup();
      BranchGroup  textRoot = new BranchGroup();

      String nearId  = "NEAR";
      Text2D nearTxt = new Text2D(nearId,
                              new Color3f(1f, 1f, 1f),
                              "Helvetica",
                              40,
                              Font.BOLD);

      String farId  = "FAR";
      Text2D farTxt = new Text2D(farId,
                              new Color3f(1f, 1f, 1f),
                              "Helvetica",
                              40,
                              Font.BOLD);

      TransformGroup objTrans = new TransformGroup();
      Transform3D t3d = new Transform3D();
      t3d.set( new Vector3d( -0.1, -0.1, -0.5 ) );
      objTrans.setTransform(t3d);


      // Build FAR text billboard
      TransformGroup farTextBillboardGrp = new TransformGroup();
      farTextBillboardGrp.setCapability( Group.ALLOW_CHILDREN_READ );
      farTextBillboardGrp.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
      farTextBillboardGrp.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
      Billboard farTextBillboard = new Billboard(farTextBillboardGrp,
                                              Billboard.ROTATE_ABOUT_POINT,
                                              new Point3f(0.0f, 0.0f, 0.0f));
      farTextBillboard.setSchedulingBounds( bounds );
      farTextBillboardGrp.addChild( farTextBillboard );
      farTextBillboardGrp.addChild( farTxt );

      // Build NEAR text billboard
      TransformGroup nearTextBillboardGrp = new TransformGroup();
      nearTextBillboardGrp.setCapability( Group.ALLOW_CHILDREN_READ );
      nearTextBillboardGrp.setCapability( TransformGroup.ALLOW_TRANSFORM_READ );
      nearTextBillboardGrp.setCapability( TransformGroup.ALLOW_TRANSFORM_WRITE );
      Billboard nearTextBillboard = new Billboard(nearTextBillboardGrp,
                                              Billboard.ROTATE_ABOUT_POINT,
                                              new Point3f(0.0f, 0.0f, 0.0f));
      nearTextBillboard.setSchedulingBounds( bounds );
      nearTextBillboardGrp.addChild( nearTextBillboard );
      nearTextBillboardGrp.addChild( nearTxt );

      objTrans.addChild( farTextBillboardGrp );
      textRoot.addChild(objTrans);

      textRoot.addChild(nearTextBillboardGrp);

      ///////////////////////////////////////////////////////////////
      // Make the method return 'textRoot' to remove the OrderedGroup
      ///////////////////////////////////////////////////////////////
//      return textRoot;

      ordGroup.addChild(textRoot);
      objRoot.addChild(ordGroup);
      return objRoot;
    }


    public void init() {
      setLayout(new BorderLayout());
           GraphicsConfiguration config =
              SimpleUniverse.getPreferredConfiguration();

           Canvas3D c = new Canvas3D(config);
      add("Center", c);

      // Create a simple scene and attach it to the virtual universe
      BranchGroup scene = createSceneGraph();
      u = new SimpleUniverse(c);

      View view = c.getView();
      view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);

      // add mouse behaviors to the ViewingPlatform
      ViewingPlatform viewingPlatform = u.getViewingPlatform();

      // This will move the ViewPlatform back a bit so the
      // objects in the scene can be viewed.
      viewingPlatform.setNominalViewingTransform();

      OrbitBehavior orbit = new OrbitBehavior(c,
                OrbitBehavior.REVERSE_ALL);
      orbit.setSchedulingBounds(bounds);
      viewingPlatform.setViewPlatformBehavior(orbit);

      u.addBranchGraph(scene);
    }


    // Running as an applet
    public OrdGroupTest() {
    }

    public void destroy() {
      u.removeAllLocales();
    }



    //
    // The following allows OrdGroupTest to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
      new MainFrame(new OrdGroupTest(), 700, 700);
    }
}
