import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math.*;
/*
<applet CODE="turnTable.class" WIDTH=502 HEIGHT=402>
</applet>
*/
public class turnTable extends Applet implements Runnable {
    Thread th=null;
    double PI=3.1416;
    double t=0.;
    double theta=0.,thetaP=0.,Y=150.;
//  Graphics g;
    Image buffer;
    Graphics bufferg;
    static int DOUBLENUM = 1;                  //GUI､ﾎﾊﾑｿﾎｿ､､��｣
    Gui_double []gui= new Gui_double[DOUBLENUM];

    public void init(){
        Button bt;
       	//g=getGraphics();
	Dimension d = getSize();
	buffer = createImage(d.width, d.height-60);
        //､ｳ､ｳ､ﾋ･鬣ﾙ･�ﾈｽ魘�ﾍ､､､��｣ｻﾈ､ｦｿﾀ､ｱｽ魘�ｽ､ｹ､�｣
	gui[0]=new Gui_double("tableSpeed 1<period<10sec","6",5);
	//gui[1]=new Gui_double("pendulum Period 0.01<Pen T<0.3sec","0.1",5);
        //
        add(bt=new Button("Draw"));
        bt.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               int i;
               for (i=0; i < DOUBLENUM ;i++){
                  gui[i].dval=new Double(gui[i].tf.getText()).doubleValue();
               }
               bufferg.setColor(Color.white);
               bufferg.fillRect(0, 0, d.width, d.height-60);
               t=0.;
               theta=0.;
               thetaP=0.;
               repaint();
           }
        });
    }
    public void update(Graphics g) {
      paint(g);
    }
    public void start(){
        if (th==null){
            th=new Thread(this);
            th.start();
        }
    }
    
    public void run(){
        while(true){
	    t=t+1.;
            theta=theta+0.01*PI/gui[0].dval;
            thetaP=thetaP+0.01*PI/0.5;
	    Y=150.*Math.cos(thetaP);
            if(t>40000.) t=0.;
            repaint();

            try {
                th.sleep(100);
            }
            catch (InterruptedException e){}
        }
    }
    class Gui_double{
        Label la;
        TextField tf;
        double dval;
        public Gui_double(String st1,String st2,int d_field){
            add(la=new Label(st1));
            add(tf=new TextField(st2,d_field));
        }
    }
    public void paint(Graphics g){
    int x,y;
	// ･ﾐ･ﾃ･ﾕ･｡､ﾎ･ｰ･鬣ﾕ･｣･ﾃ･ｯ･ｳ･ﾆ･ｭ･ｹ･ﾈ､霹ﾀ､ｹ､
    if (bufferg == null)
      bufferg = buffer.getGraphics();    
	Dimension d = getSize(); 
//	bufferg.setColor(Color.white);
//    	bufferg.fillRect(0, 0, d.width, d.height-60);
	bufferg.setColor(Color.black);

    if(Y>0.){
	x=-(int)(Y*Math.sin(theta));
	y=(int)(Y*Math.cos(theta));
    }
    else {
	x=-(int)(-Y*Math.sin(PI+theta));
        y=(int)(-Y*Math.cos(PI+theta));
    }
    bufferg.drawLine(190,200,210,200);
    bufferg.drawLine(200,190,200,210);
    bufferg.fillOval(x+200, y+200, 5, 5);
//    bufferg.fillOval((int)Y,(int)Y,5,5);

              g.drawImage(buffer,0,60,this);
    }
}
