import java.applet.*;
import java.awt.*;
import javax.swing.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;


// Bug repro program.  Billboarded color cube does not display
// when in ROTATE_ABOUT_POINT mode, but will display if the
// applet size in HTML tag is changed from 600x600 to 500x500.
public class BillBoardTestApplet extends JApplet
{
  private SimpleUniverse universe;
  private BoundingSphere infiniteBounds =
    new BoundingSphere( new Point3d(), Double.MAX_VALUE );

  public BillBoardTestApplet()
  {
    getContentPane().setLayout( new BorderLayout() );
  }


  public void init()
  {

    GraphicsConfiguration gcfg =
       SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas = new Canvas3D(gcfg);
    getContentPane().add( canvas, BorderLayout.CENTER );
    universe = new SimpleUniverse( canvas );

    // This will move the ViewPlatform back a bit so the
    // objects in the scene can be viewed.
    universe.getViewingPlatform().setNominalViewingTransform();

    // Create the scene with just a ColorCube, either billboarded
    // about a point or an axis.  Seems to be broken when
    // set to true.
    boolean rotatePoint = true;       // TURN THIS ON AND OFF -Lee
    BranchGroup mainBranch = createScene( rotatePoint );
    mainBranch.compile();

    universe.addBranchGraph( mainBranch );
  }

  public void start()
  {
  }

  BranchGroup createScene(boolean rotatePoint)
  {
    // Create the main branch to be returned.
    BranchGroup bg = new BranchGroup();

    // Scale the scene by 1/10.
    Transform3D xform = new Transform3D();
    xform.setScale(0.1f);
    TransformGroup tg = new TransformGroup( xform );
    bg.addChild( tg );

    // Create an editable transform with a color cube.
    TransformGroup bbgrp = new TransformGroup();
    bbgrp.setCapability( bbgrp.ALLOW_TRANSFORM_WRITE );
    bbgrp.addChild( new ColorCube() );

    // Create a billboard for the colorcube.
    Billboard bb = new Billboard( bbgrp );
    bb.setRotationPoint( new Point3f( 0f, 0f, 0f ) );
    bb.setAlignmentAxis( new Vector3f( 0f, 1f, 0f ) );
    bb.setSchedulingBounds(infiniteBounds);
    bb.setEnable( true );


    if( rotatePoint )
      bb.setAlignmentMode( bb.ROTATE_ABOUT_POINT );
    else
      bb.setAlignmentMode( bb.ROTATE_ABOUT_AXIS );


    bg.addChild( bb );
    tg.addChild(bbgrp);


    return( bg );
  }

}
