import java.net.*;
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import javax.vecmath.*;
import javax.media.j3d.*;
import javax.swing.*;

import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.universe.ViewingPlatform;
import com.sun.j3d.utils.applet.MainFrame;

import com.sun.j3d.utils.geometry.*;

public class VisibleTest extends Applet implements ActionListener
{
  private TransformGroup systemGroup = null;

  private SimpleUniverse universe = null;
  private RenderingAttributes[] boxRender = new RenderingAttributes[3];

  static public void main(String[] args)
  {
    new MainFrame(new VisibleTest(), 420, 300);
  }

  public VisibleTest()
  {
  }

  public void init()
  {
    setLayout(new BorderLayout());

    // Create the canvas
    GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
    GraphicsConfiguration gcfg =
              GraphicsEnvironment.getLocalGraphicsEnvironment().
              getDefaultScreenDevice().getBestConfiguration(template);
    Canvas3D canvas = new Canvas3D(gcfg);
    canvas.setSize(300, 300);
    add("Center", canvas);

    // Add buttons to toggle the view of boxes.
    JButton but0 = new JButton("Toggle0");
    but0.addActionListener(this);
    add("South", but0);
    JButton but1 = new JButton("Toggle2");
    but1.addActionListener(this);
    add("West", but1);
    JButton but2 = new JButton("Toggle1");
    but2.addActionListener(this);
    add("East", but2);

    // Create the universe.
    universe = new SimpleUniverse(canvas);

    // Create the main branch.
    BranchGroup mainBranch = new BranchGroup();

    makeBox(0, mainBranch, 0.0f, false);
    makeBox(1, mainBranch, 3.2f, true);
    makeBox(2, mainBranch, -3.2f, true);


    // Make the main branch live.
    universe.addBranchGraph(mainBranch);
  }

  private void makeBox(int which, BranchGroup branch, float xloc, boolean visible)
  {

    Appearance app = new Appearance();

    boxRender[which] = new RenderingAttributes();
    boxRender[which].setCapability(RenderingAttributes.ALLOW_VISIBLE_WRITE);
    boxRender[which].setCapability(RenderingAttributes.ALLOW_VISIBLE_READ);
    boxRender[which].setVisible(visible);
    app.setRenderingAttributes(boxRender[which]);

    PolygonAttributes poly = new PolygonAttributes();

    poly.setPolygonMode(poly.POLYGON_LINE);

    app.setPolygonAttributes(poly);

    Transform3D xform = new Transform3D();
    xform.setTranslation(new Vector3f(xloc, 0.0f, -10.0f));
    TransformGroup grp = new TransformGroup(xform);
    TransformGroup grp2 = new TransformGroup();
    grp2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    RotationInterpolator rot =
      new RotationInterpolator(new Alpha(-1,5000), grp2);
    rot.setSchedulingBounds(new BoundingSphere(new Point3d(), 5000.0) );
    rot.setEnable(true);
    grp2.addChild(new com.sun.j3d.utils.geometry.Box(1.0f, 1.0f, 1.0f, app));
    grp.addChild(grp2);
    branch.addChild(grp);
    branch.addChild(rot);


  }

  public void actionPerformed(ActionEvent e)
  {
    String command = e.getActionCommand();
    if (command.startsWith("Toggle"))
    {
      int which = Integer.parseInt( command.substring(command.length()-1) );
      System.out.println("Toggling " + which + " visible=" +
        !boxRender[which].getVisible() );
      boxRender[which].setVisible( !boxRender[which].getVisible() );
    }
  }


}
