import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.lang.Math.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.GraphicsConfiguration;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;

abstract class Anime3DApplet extends JApplet{
    Canvas3D canvas;
    JCheckBox stopCB;
    JSlider speedBar;
    JButton restartButton;
    JPanel animePanel;
    javax.swing.Timer tm;
    SimpleUniverse u;

    BranchGroup scene;

    double t=0.0;
    double stepT=0.02;

    public void setStepTime(double a) {
	stepT=a;
    }
    public void destroy() {
        u.cleanup();
    }

    public void init(){
        Container cont=getContentPane();
        cont.setLayout(new BorderLayout());
        
        
        GraphicsConfiguration config =SimpleUniverse.getPreferredConfiguration();
        
        canvas = new Canvas3D(config);
        
        scene = new BranchGroup();


        u = new SimpleUniverse(canvas);
        u.getViewingPlatform().setNominalViewingTransform();

        scene.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
        scene.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
        scene.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
        scene.setCapability(BranchGroup.ALLOW_DETACH);

        tm= new javax.swing.Timer(50,
                                  new ActionListener() {
                                      public void actionPerformed(ActionEvent e){
                                          t=t+stepT;
                                          repaint();
                                          writeCanvas();
                                      }
                                  }
                                  );

        getRootPane().setDoubleBuffered(true);
        

        speedBar = new JSlider(0,50,10);
        speedBar.setMajorTickSpacing(10);
        speedBar.setMinorTickSpacing(1);
        speedBar.setPaintTicks(true);
        speedBar.setPaintLabels(true);

        speedBar.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                int k=speedBar.getValue();
                if( k==0 ) {
                    tm.stop();
                } else {
                    tm.setDelay(1000/k);
                    tm.restart();
                }
            }
        });
	

        stopCB=new JCheckBox("中断");
        restartButton=new JButton("もう一度最初から");

    stopCB.addItemListener(new ItemListener() {
	  public void  itemStateChanged(ItemEvent evt) {
	    if( stopCB.isSelected() ) {
	      tm.stop();
	    } else {
	      tm.start();
	    }
	  }
	});
      
    restartButton.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent evt) {
	  t=0;
	  tm.start();
	  stopCB.setSelected(false);
	}
      });     
    
    Label l=new Label("コマ数／秒");

    JPanel animePanelSub=new JPanel();
    animePanelSub.setLayout( new BorderLayout());
    animePanelSub.add(l,BorderLayout.WEST);
    animePanelSub.add(speedBar,BorderLayout.CENTER);


    animePanel = new JPanel();
    animePanel.setLayout( new BorderLayout());
    animePanel.add(stopCB,BorderLayout.WEST);
    animePanel.add(animePanelSub,BorderLayout.CENTER);
    animePanel.add(restartButton,BorderLayout.EAST);
  
    cont.add("Center",canvas);
  }

  abstract void writeCanvas();

}


	
   
