/*sin11.java
 *1997/7/7
 *Suzuki,Tetsuo —é–Ø“O•v
 */


import java.applet.*;
import java.awt.*;
import java.lang.Math;

public class sin11  extends Applet implements Runnable{
	Thread th=null;
	int x,y;
	int v;
	int dv;
	int av;
	int f;
	int r,l,m;
	int a,b,c,e;
	//float c;
	Image buffer;
	Dimension d;
	
	public void init(){
		add(new Label("phase"));
		CheckboxGroup cg0=new CheckboxGroup();
		add(new Checkbox("a",cg0,true));
		add(new Checkbox("b",cg0,false));
		add(new Label("display"));
		CheckboxGroup cg=new CheckboxGroup();
		add(new Checkbox("1",cg,true));
		add(new Checkbox("2",cg,false));
		add(new Checkbox("3",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();
		f=1;
		v=0;
		av=10;	
		dv=av;
		r=10;
		l=240;
		m=3;
		a=1000;
		b=1000;
		c=1;
		e=1;	
		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.yellow);
		g.fillRect(200+(2*m+c)*l/4+r/2,30,50,d.height);
		g.setColor(Color.black);
		g.drawLine(0,(int)(d.height/2)+r/2,200+(2*m+c)*l/4+r/2,(int)(d.height/2)+r/2);
		for (x=200;x>=0;x=x-r){
			y=(int)((d.height/6)*Math.sin((v+x-200)*3.1415*2/l)+(int)(d.height/2));
		if (x==200)
		g.setColor(Color.black);
		else
		g.setColor(Color.blue);
		g.fillOval(x,y,r,r);
		
		}
		for (x=0;x<=200+(2*m+c)*l/4;x=x+r){
			y=(int)((d.height/6)*e*Math.sin((v+x-200)*3.1415*2/l)+(int)(d.height/2)+a);
		g.setColor(Color.green);
		g.fillOval(x,y,r,r);
		
		}
		for (x=200;x<=200+(2*m+c)*l/4;x=x+r){
			y=(int)((d.height/6)*Math.sin((v-x+200)*3.1415*2/l)+(int)(d.height/2));		
		if (x==200)
		g.setColor(Color.black);
		else
		g.setColor(Color.blue);
		g.fillOval(x,y,r,r);
		
		}
		
		for (x=200;x>=0;x=x-r){
			y=(int)(b+e*(d.height/6)*Math.sin((v+x-200)*3.1415*2/l)+(d.height/6)*Math.sin((v+x-200)*3.1415*2/l)+(int)(d.height/2));
		
		g.setColor(Color.red);
		g.fillOval(x,y,r,r);
		
		}
		for (x=200;x<=200+(2*m+c)*l/4;x=x+r){
			y=(int)(b+e*(d.height/6)*Math.sin((v+x-200)*3.1415*2/l)+(d.height/6)*Math.sin((v-x+200)*3.1415*2/l)+(int)(d.height/2));
		
		g.setColor(Color.red);
		g.fillOval(x,y,r,r);
		
		}
	}
	void move (){
		dv=f*av;
		v=v+dv;
		
		
	}

	public void start(){
		if (th==null){
			th=new Thread(this);
			th.start();
		}
		
	}
	public boolean action(Event ev,Object o){
		/*Button b=(Button)ev.target;
		if ("start".equals (b.getLabel()))
			th.resume();
		else if ("stop".equals(b.getLabel()))
			th.suspend();*/
		Checkbox ch=(Checkbox)ev.target;
		if ("a".equals (ch.getLabel())){
			c=1;
			e=1;
			}
		else if("b".equals (ch.getLabel())){
		        c=2;
			e=-1;
			}
		if ("1".equals (ch.getLabel())){
			a=1000;
			b=1000;}
		else if("2".equals (ch.getLabel())){
		        a=0;
			b=1000;}
		else if("3".equals (ch.getLabel())){
		        a=0;
			b=0;}
		else if ("high".equals (ch.getLabel()))
			av=10;	   
		else if("low".equals (ch.getLabel()))
			av=5;	
		else if("stop".equals (ch.getLabel()))
			av=0;	 
		else if ("S".equals (ch.getLabel()))
			r=5;	   
		else if("L".equals (ch.getLabel()))
			r=10;	 
		//try{Thread.sleep(5000);}
		//	catch(InterruptedException e){}
			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;
		}
	}
}
