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.,Y=200;
//  Graphics g;
    Image buffer;
    Graphics bufferg;
    static int DOUBLENUM = 2;                  //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("ballSpeed 1<nu<10cm/sec","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();
               }
               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;
	    Y=Y-gui[1].dval;
	    if(Y < -200.){
		 Y=200.;
		theta=0.;
	    }
            if(t>400.) 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);
    }
}
