/*
	Yi & Eight Gua Applet
	Author: Faster Lu
		faster@pacific.net.sg

	The size of the applet must be bug enough
*/



import java.awt.*;
import java.lang.*;

public class YApplet extends java.applet.Applet implements Runnable{
	Guaset guaset=new Guaset();
	Scrollbar scrollbar;
	int r,xx,yy,speed;
	Color c1,c2;
	int ca,cstep;
	Thread tr;
	int redraw=1;
	Image backBuffer; 
    	Graphics bg; 

    	public void init() {
		String att = getParameter("speed");
		speed = (att == null) ? 400 : (5000 / Integer.valueOf(att).intValue());
		cstep = 5;

		setBackground(Color.orange);
		setFont(new Font("Dialog",Font.BOLD,12));
		setLayout(new BorderLayout());
		add("East",scrollbar=new Scrollbar(Scrollbar.VERTICAL,5,0,5,45));
        	try  {
           		backBuffer = createImage(size().width,size().height); 
           		bg = backBuffer.getGraphics();
        	} catch (Exception e) {bg = null;} 

		c1=Color.white;
		c2=Color.black;

		ca=-90;
	}

	public void start() {
		tr = new Thread(this);
		tr.start();
    	}

	public void stop() {
		tr.stop();
    	}

	public void run() {
		while (true) {
			try {Thread.currentThread().sleep(speed);} catch (InterruptedException e){}
			Graphics g=getGraphics();

			if (g!=null)	{
				if (redraw==1)	{
					bg.setColor(Color.orange);
					bg.fillRect(0,0,size().width,size().height);
					guaset.draw(bg);
					bg.setColor(Color.blue);
					bg.drawString("Speed",size().width-70,size().height-20);
				}
	        		paintyi(bg);
		        	g.drawImage(backBuffer,0,0,this); 
			}
		}
	}

	private void fillcircle(int x0,int y0,int r,int a1,int a2,Color cc,Graphics g)	{
		g.setColor(cc);
		g.fillArc(x0-r,y0-r,r+r,r+r,a1,a2);
	}

    	public void paintyi(Graphics g) {
		int tx,ty,ox,oy,ax,ay;
		float pi=3.1415926f;

		tx=(int) (Math.round(r*Math.cos(ca*pi/180)/2));
		ty=(int) (Math.round(r*Math.sin(ca*pi/180)/2));
		ax=(int) (Math.round(r*Math.cos(ca*pi/180)/2));
		ay=(int) (Math.round(r*Math.sin(ca*pi/180)/2));

		fillcircle(xx,yy,r,ca,181,c2,g);
		fillcircle(xx,yy,r,ca+181,181,c1,g);

		fillcircle(xx-ax,yy+ay,r/2,ca+180,181,c2,g);
		fillcircle(xx+ax,yy-ay,r/2,ca,181,c1,g);

		fillcircle(xx+tx,yy-ty,r/6,0,360,c2,g);
		fillcircle(xx-tx,yy+ty,r/6,0,360,c1,g);

		ca+=cstep;
		if (ca>=270)
			ca=-90;
	}


    	public void paint(Graphics g) {
		g.setColor(Color.orange);
		g.fillRect(0,0,size().width,size().height);

		r=Math.min(size().height,size().width-70)*1/3;
		xx=size().width/2;
		yy=size().height/2;

		redraw=1;

		guaset.set(xx,yy,r*4/3,r/4,r/6,r/8);

		guaset.draw(g);

		g.setColor(Color.blue);
		g.drawString("Speed",size().width-70,size().height-20);

		paintyi(g);
	}

	public void update(Graphics g)	{
		paint(g);
	}

	public boolean handleEvent(Event evt)	{
		if (evt.target instanceof Scrollbar)	{
			cstep=scrollbar.getValue();
			cstep=cstep/5*5;
			return true;
		} else
			return false;
	}
}


class Gua	{
	int len1,len2,gap;

	public void set(int sz1,int sz2,int sz3)	{
		len1=sz1;
		len2=sz2;
		gap=sz3;
	}

	public void yao(int w,int x,int y,int d,Graphics g)	{
		float sq2=0.7071f;

		if (w==0)	{
			switch (d)	{
				case	0:
					g.drawLine(x,y-len1,x,y+len1);
					break;
				case	1:
					g.drawLine(Math.round(x-len1*sq2),Math.round(y-len1*sq2),Math.round(x+len1*sq2),Math.round(y+len1*sq2));
					break;
				case	2:
					g.drawLine(x-len1,y,x+len1,y);
					break;
				case	3:
					g.drawLine(Math.round(x-len1*sq2),Math.round(y+len1*sq2),Math.round(x+len1*sq2),Math.round(y-len1*sq2));
					break;
			}
		} else	{
			switch (d)	{
				case	2:
					g.drawLine(x-len1,y,x-len1+len2,y);
					g.drawLine(x+len1-len2,y,x+len1,y);
					break;
				case	3:
					g.drawLine(Math.round(x-len1*sq2),Math.round(y+len1*sq2),Math.round(x-(len1-len2)*sq2),Math.round(y+(len1-len2)*sq2));
					g.drawLine(Math.round(x+len1*sq2),Math.round(y-len1*sq2),Math.round(x+(len1-len2)*sq2),Math.round(y-(len1-len2)*sq2));
					break;
				case	0:
					g.drawLine(x,y-len1,x,y-len1+len2);
					g.drawLine(x,y+len1-len2,x,y+len1);
					break;
				case	1:
					g.drawLine(Math.round(x-len1*sq2),Math.round(y-len1*sq2),Math.round(x-(len1-len2)*sq2),Math.round(y-(len1-len2)*sq2));
					g.drawLine(Math.round(x+len1*sq2),Math.round(y+len1*sq2),Math.round(x+(len1-len2)*sq2),Math.round(y+(len1-len2)*sq2));
					break;
			}
		}
	}

	public void draw(int w,int x0,int y0,int r,int d,Graphics g)	{
		int x,y;
		float pi=3.1415926f;

		g.setColor(Color.red);

		x=(int) (x0+Math.round((r-gap)*Math.cos(d*pi/4)));
		y=(int) (y0-Math.round((r-gap)*Math.sin(d*pi/4)));
		yao(w & 4,x,y,d%4,g);

		x=(int) (x0+Math.round(r*Math.cos(d*pi/4)));
		y=(int) (y0-Math.round(r*Math.sin(d*pi/4)));
		yao(w & 2,x,y,d%4,g);

		x=(int) (x0+Math.round((r+gap)*Math.cos(d*pi/4)));
		y=(int) (y0-Math.round((r+gap)*Math.sin(d*pi/4)));
		yao(w & 1,x,y,d%4,g);
	}
}


class Guaset	{
	int x,y,r;
	Gua gua=new Gua();

	public void set(int x0,int y0,int r0,int sz1,int sz2,int sz3)	{
		x=x0;
		y=y0;
		r=r0;
		gua.set(sz1,sz2,sz3);
	}

	public void draw(Graphics g)	{
		gua.draw(0,x,y,r,2,g);
		gua.draw(1,x,y,r,3,g);
		gua.draw(2,x,y,r,4,g);
		gua.draw(3,x,y,r,5,g);
		gua.draw(4,x,y,r,1,g);
		gua.draw(5,x,y,r,0,g);
		gua.draw(6,x,y,r,7,g);
		gua.draw(7,x,y,r,6,g);
	}
}
