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 Text2dTest extends Applet {

    private SimpleUniverse u;

    private Text2D text = null;

    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();

      // Create the transform group node and initialize it to the
      // identity.  Enable the TRANSFORM_WRITE capability so that
      // our behavior code can modify it at runtime.  Add it to the
      // root of the subgraph.
      TransformGroup objTrans = new TransformGroup();
      objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
      objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
      objRoot.addChild(objTrans);

      text = new Text2D( "Java", new Color3f(1f, 1f, 1f),
                         "Helvetica", 80, Font.BOLD );
      text.setCapability( Shape3D.ALLOW_APPEARANCE_READ );
      text.getAppearance().setCapability( Appearance.ALLOW_TEXTURE_READ );
      text.getAppearance().getTexture().setCapability(
         Texture.ALLOW_IMAGE_WRITE );

      objTrans.addChild(text);

      // Set up the background
      Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
      Background bgNode = new Background(bgColor);
      bgNode.setApplicationBounds(bounds);
      objRoot.addChild(bgNode);

      return objRoot;
    }


    public void init() {
      setLayout(new BorderLayout());
      GraphicsConfiguration config =
         SimpleUniverse.getPreferredConfiguration();

      Canvas3D c = new Canvas3D(config);
      add("Center", c);

      c.addMouseListener( new MouseAdapter()
           {
              public void mouseClicked( MouseEvent evt )
              {
                 if( text.getString().equals("Java") )
                 {
                    text.setString("3D");
                 }
                 else
                 {
                    text.setString("Java");
                 }
              }
           } );

      // Create a simple scene and attach it to the virtual universe
      BranchGroup scene = createSceneGraph();
      u = new SimpleUniverse(c);

      // 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();

      u.addBranchGraph(scene);
    }

    // Caled if running as a program
    public Text2dTest() {
    }

    public void destroy() {
      u.removeAllLocales();
    }



    //
    // The following allows ObjLoad to be run as an application
    // as well as an applet
    //
    public static void main(String[] args) {
      new MainFrame(new Text2dTest(), 200, 200);
    }
}
