/*stwave.java
 *1997/7/7
 *Suzuki,Tetsuo
 */

import java.applet.*;
import java.awt.*;
import java.lang.Math;

public class stwave  extends Applet implements Runnable{
	Thread th=null;
	int x,y,n;
	int v;
	int dv;
	int av;
	int f;
	int r;
	int t;
	int dt;
	Image buffer;
	Dimension d;
	
	public void init(){
		add(new Label("node"));
		CheckboxGroup cg=new CheckboxGroup();
		add(new Checkbox("1",cg,false));
		add(new Checkbox("2",cg,true));
		add(new Checkbox("3",cg,false));
		add(new Checkbox("4",cg,false));
		add(new Label("speed"));
		CheckboxGroup cg1=new CheckboxGroup();
		add(new Checkbox("high",cg1,true));
		add(new Checkbox("low",cg1,false));
		add(new Checkbox("stop",cg1,false));
		add(new Label("scale"));
		CheckboxGroup cg2=new CheckboxGroup();
		add(new Checkbox("S",cg2,false));
		add(new Checkbox("L",cg2,true));
		//add(new Button("pause"));
		d=size();
		v=5;
		f=1;
		n=2;
		av=10;	
		dv=2;
		t=10;
		dt=5;
		r=20;
		buffer=createImage(d.width,d.height);
	}

	void drawToBuffer(){
		Graphics g=buffer.getGraphics();
		g.setColor(Color.white);
		g.clipRect(0,30,d.width,d.height-30);
		g.fillRect(0,30,d.width,d.height-30);
		g.setColor(Color.black);
		g.drawLine(200+r/2,30,200+r/2,d.height);
		//g.drawLine(0,(int)(d.height/2)+r/2,d.width,(int)(d.height/2)+r/2);
		g.drawLine(0,(int)(d.height/4)+r/2,d.width,(int)(d.height/4)+r/2);
		for (x=0;x<=720;x=x+r){
			y=(int)(Math.sin(50*v)*(d.height/4)*Math.sin(x*2*3.14159/(1440/n))+x+r/2);	
	        //if (x!=200)
		g.setColor(Color.blue);
		//else
		//g.setColor(Color.red);
		g.fillRect(y,(int)(d.height*3/4)-3*r,r/2,7*r);
		}
		for (x=0;x<=720;x=x+r){
			y=(int)(-Math.sin(50*v)*(d.height/8)*Math.sin(x*2*3.14159/(1440/n))+(int)(d.height/4));	
		//if (x==200)
	       // g.setColor(Color.red);
		//else
		g.setColor(Color.blue);
		g.fillOval(x,y,r,r);
		}
	
	}
	void move (){
		
		v=v+dv;
		t=t+dt;
		
	}

	public void start(){
		if (th==null){
			th=new Thread(this);
			th.start();
		}
		
	}
	public boolean action(Event ev,Object o){
		
		Checkbox ch=(Checkbox)ev.target;
		if ("1".equals (ch.getLabel()))
			n=1;
		else if("2".equals (ch.getLabel()))
		        n=2;
		if ("3".equals (ch.getLabel()))
			n=3;
		else if("4".equals (ch.getLabel()))
		        n=4;
		else if ("high".equals (ch.getLabel()))
			dv=2;	   
		else if("low".equals (ch.getLabel()))
			dv=1;	
		else if("stop".equals (ch.getLabel()))
			dv=0;	 
		else if ("S".equals (ch.getLabel()))
			r=10;	   
		else if("L".equals (ch.getLabel()))
			r=20;	
		if (ev.target instanceof Button){	 
		try{Thread.sleep(5000);}
			catch(InterruptedException e){}  
			return true;}
			return true;		
		}	

	public void run(){
		while(th!=null){
		move();
		drawToBuffer();		
		repaint();
		try {
				Thread.sleep(20);
			}
			catch (InterruptedException e){}
		}
	}
        public void update(Graphics g){
		paint(g);
	}

	public void paint (Graphics g){
		g.drawImage(buffer,0,0,this);
	}
		
	
	public void stop(){
		if (th!=null){
			th.stop();
			th=null;
		}
	}
}
