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

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 TestBed1 extends Applet
{

  // CHANGE THIS VALUE TO TEST DISTANCE ATTENUATION
  private float distanceToSound = 45.0f;

  // THE GAIN CURVE... (distance,gain) pairs.
  private static Point2f[] distanceGain = new Point2f[]
  {
    new Point2f(0.0f, 1.0f),
    new Point2f(50.0f, 0.0f)
  };

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

  TestBed1()
  {
  }

  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);

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

    // Initialize sound capability.
    AudioDevice audioDev = universe.getViewer().createAudioDevice();

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

    // Put a color cube in the scene just for the "halibut".
    Transform3D xform = new Transform3D();
        xform.setTranslation(new Vector3f(0.0f, 0.0f, -10.0f));
    TransformGroup grp = new TransformGroup(xform);
        grp.addChild(new ColorCube());
    mainBranch.addChild(grp);

    // The sound.
    try
    {
      URL soundURL = new URL(getDocumentBase(), "Crickets.wav");
      MediaContainer soundMedia = new MediaContainer(soundURL);
      Point3f soundPos = new Point3f(0.0f, 0.0f, -distanceToSound);
      PointSound sound = new PointSound(soundMedia, 1.0f, soundPos);
          sound.setDistanceGain(distanceGain);
          sound.setEnable(true);
      Bounds bounds =
        new BoundingSphere(new Point3d(soundPos), 5000.0);
      sound.setLoop(-1);
      sound.setSchedulingBounds(bounds);
      mainBranch.addChild(sound);
    }
    catch ( MalformedURLException exc )
    {
      System.out.println("Could not open sound file!");
    }

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

