import java.applet.Applet;
import java.util.Date;
import java.awt.Event;
import java.awt.Font;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.net.MalformedURLException;
import java.net.URL;

public class cyber extends Applet
{
	Color OrgBlue = new Color (0x3366cc), CodeRed = new Color (0xCC0000);
	Image BackGround, Circle, Circles, PureArea;
	Graphics BackGroundGraphics, CircleGraphics, PureAreaGraphics;
	int Width = 140, Height = 260, RectX = 63, RectWidth = 32, RectHeight = 16, RectDistance = 16, BigBoxWidth = 96, LineX = 90, CircleY = 233, CircleX = 82, Diameter = 17, Stage = 0, BlinkCounter = 16, WholeBlink = 16, WhichBox = 0, LastBox = 0, PureAreaX = 97, PureAreaY = 12 * RectHeight + 1;
	boolean NotMoving = true;
	Font CodeFont = new Font ("Dialog", Font.BOLD, 10);
	String [] Messages = {"Who:", "What:", "Where:", "When:", "How:", "Why:"};

	public void init()
	{
		MediaTracker M = new MediaTracker (this);
		Circles = this.getImage (this.getDocumentBase (), "blink.gif");
		M.addImage (Circles, 0);
		BackGround = createImage (Width, Height);
		BackGroundGraphics = BackGround.getGraphics();
		BackGroundGraphics.setColor(OrgBlue);
		BackGroundGraphics.fillRect (0, 0, Width, Height);
		BackGroundGraphics.setColor(Color.black);
		BackGroundGraphics.drawLine (LineX, 0, LineX, Height);
		DrawBox (0, 12 * RectHeight, BigBoxWidth, Color.white);
		Circle = createImage (Diameter, Diameter);
		CircleGraphics = Circle.getGraphics();
		BackGroundGraphics.setFont (CodeFont);
		PureArea = createImage (RectWidth + 2, RectHeight + 1);
		PureAreaGraphics = PureArea.getGraphics();
		PureAreaGraphics.setColor (OrgBlue);
		PureAreaGraphics.fillRect (0, 0, RectWidth + 2, RectHeight + 1);
		try {M.waitForAll();} catch (InterruptedException e){}
		CircleGraphics.drawImage (Circles, - Diameter * 16, 0, this);
		BackGroundGraphics.drawImage (Circle, CircleX, CircleY, this);
	}

	public void BuildBoxes()
	{
		BackGroundGraphics.setColor(Color.black);
		for (int i = 0; i < 6; i++)
		{
			BackGroundGraphics.drawLine (LineX-1,i * 2 * RectHeight,LineX-1,i * 2 * RectHeight+RectHeight);
			BackGroundGraphics.drawLine (LineX+1,i * 2 * RectHeight,LineX+1,i * 2 * RectHeight+RectHeight);
		}
		UpdateScreen(RectX, 0, RectWidth + 4, 11 * Height);
		Pause (50);
		for (int i = 0; i < 6; i++)
		{
			BackGroundGraphics.setColor(Color.black);
			BackGroundGraphics.drawLine (LineX-2,i * 2 * RectHeight,LineX-2,i * 2 * RectHeight+RectHeight);
			BackGroundGraphics.drawLine (LineX+2,i * 2 * RectHeight,LineX+2,i * 2 * RectHeight+RectHeight);
			BackGroundGraphics.setColor(Color.white);
			BackGroundGraphics.drawLine (LineX,i * 2 * RectHeight + 2,LineX,i * 2 * RectHeight+RectHeight - 2);
		}
		UpdateScreen(RectX, 0, RectWidth + 4, 11 * Height);
		Pause (50);
		for (int j = 0; j < 4; j++)
		{
			for (int i = 0; i < 6; i++)
			{
				BackGroundGraphics.setColor(Color.black);
				BackGroundGraphics.drawLine (LineX-3-j,i * 2 * RectHeight,LineX-3-j,i * 2 * 	RectHeight+RectHeight);
				BackGroundGraphics.drawLine (LineX+3+j,i * 2 * RectHeight,LineX+3+j,i * 2 * RectHeight+RectHeight);
				BackGroundGraphics.setColor(Color.white);
				BackGroundGraphics.drawLine (LineX+1+j,i * 2 * RectHeight + 2,LineX+1+j,i * 2 * RectHeight+RectHeight - 2);
				BackGroundGraphics.drawLine (LineX-1-j,i * 2 * RectHeight + 2,LineX-1-j,i * 2 * RectHeight+RectHeight - 2);
			}
			UpdateScreen(RectX, 0, RectWidth + 4, 11 * Height);
			Pause (50);
		}
		for (int j = 12; j < RectWidth+1; j++)
		{
			for (int i = 0; i < 6; i++) DrawBox (RectX + RectWidth - j, i * 2 * RectHeight, j, Color.white);
			UpdateScreen(RectX, 0, RectWidth + 4, 11 * Height);
			Pause (50);
		}
		NotMoving = true;
	}

	public void DrawBox (int x, int y, int width, Color Fill)
	{
		BackGroundGraphics.setColor(Color.black);
		BackGroundGraphics.drawRect (x, y, width, RectHeight);
		BackGroundGraphics.drawRect (x+1, y+1, width, RectHeight-2);
		BackGroundGraphics.setColor(Fill);
		BackGroundGraphics.fillRect (x+2, y+2, width-2, RectHeight-3);
	}

	public boolean InsideCircle (int x, int y)
	{
		if (Math.pow(x - CircleX - Diameter/2, 2)+Math.pow(y - CircleY - Diameter/2, 2) < Diameter*Diameter/4)
		return true;
		else return false;
	}

	public boolean InsideBox (int x, int y)
	{
		WhichBox = 0;
		boolean B = false;
		for (int i = 0; i < 6; i++)
		if ((x > RectX) && (x < RectX + RectWidth) && (y > i * 2 * RectHeight) && (y < i * 2 * RectHeight + RectHeight))
		{
			WhichBox = i + 1;
			B = true;
		}
		return B;
	}

	public void Pause(int MS)
	{
		long MS1 = new Date().getTime();
		long MS2 = new Date().getTime();
		while (MS2 < MS1 + MS) MS2 = new Date().getTime();
	}

	public void OneBlink(int i, int p)
	{
		CircleGraphics.drawImage (Circles, - Diameter * i, 0, this);
		BackGroundGraphics.drawImage (Circle, CircleX, CircleY, this);
		UpdateScreen (CircleX, CircleY, Diameter, Diameter);
		Pause (p);
		BlinkCounter++;
	}

	public void WholeBlink(int p)
	{
		while (BlinkCounter < WholeBlink) OneBlink(BlinkCounter, p);
		NotMoving = true;
	}

	public void MoveBox()
	{
		BlinkCounter = 0;
		BackGroundGraphics.drawImage (PureArea, PureAreaX + 1, PureAreaY - 1, this);
		UpdateScreen(PureAreaX + 1, PureAreaY - 1, RectWidth + 2, RectHeight + 2);
		for (int i = RectX; i < PureAreaX; i++)
		{
			DrawBox (RectX, (WhichBox - 1) * 2 * RectHeight, RectWidth, Color.white);
			DrawBox (i + 1, (WhichBox - 1) * 2 * RectHeight, RectWidth, CodeRed);
			UpdateScreen(RectX, (WhichBox - 1) * 2 * RectHeight, Width - RectX, RectHeight + 2);
			OneBlink (BlinkCounter, 10);
			if (BlinkCounter == WholeBlink) BlinkCounter = 0;
		}
		for (int i = (WhichBox - 1) * 2 * RectHeight; i < PureAreaY; i++)
		{
			BackGroundGraphics.drawImage (PureArea, PureAreaX, i - 1, this);
			DrawBox (PureAreaX, i, RectWidth, CodeRed);
			UpdateScreen(PureAreaX, i - 1, Width - PureAreaX, RectHeight + 4);
			Pause (2 * WhichBox);
			OneBlink (BlinkCounter, 0);
			if (BlinkCounter == WholeBlink) BlinkCounter = 0;
		}
		WholeBlink(50);
	}

	public void DrawMessage (int i)
	{
		BackGroundGraphics.setColor(Color.black);
		BackGroundGraphics.drawString (Messages[i], 30, 13 * RectHeight - 3);
	}

	public void FillBoxes()
	{
		DrawBox (RectX, (LastBox - 1) * 2 * RectHeight, RectWidth, Color.white);
		DrawBox (RectX, (WhichBox - 1) * 2 * RectHeight, RectWidth, CodeRed);
		UpdateScreen(RectX, (LastBox - 1) * 2 * RectHeight, RectWidth + 2, RectHeight + 2);
		UpdateScreen(0, (WhichBox - 1) * 2 * RectHeight, RectWidth + 2, RectHeight + 2);
		DrawBox (0, 12 * RectHeight, BigBoxWidth, Color.white);
		DrawMessage(WhichBox-1);
		UpdateScreen(0, 12 * RectHeight, BigBoxWidth, RectHeight);
		LastBox = WhichBox;
		OneBlink(15, 0);
	}

	public void ClearBoxes()
	{
		DrawBox (RectX, (LastBox - 1) * 2 * RectHeight, RectWidth, Color.white);
		DrawBox (0, 12 * RectHeight, BigBoxWidth, Color.white);
		UpdateScreen(RectX, (LastBox - 1) * 2 * RectHeight, RectWidth + 2, RectHeight + 2);
		OneBlink(16, 0);
	}

	public boolean mouseMove (Event e, int x, int y)
	{
		if (NotMoving)
		{
			if (Stage < 3)
			{
				if (InsideCircle(x,y)) OneBlink(15, 0);
				if (InsideCircle(x,y)==false) OneBlink(16, 0);
			}
			if (Stage > 2)
			{
				if (InsideBox(x,y)) FillBoxes ();
				if ((InsideBox(x,y) == false)&&(LastBox > 0)) ClearBoxes ();
			}
		}
		return true;
	}

	public boolean mouseUp (Event e, int x, int y)
	{
		if (NotMoving)
		{
			if ((Stage < 3)&&(InsideCircle (x, y)))
			{
				if (Stage == 2)
				{
					NotMoving = false;
					BlinkCounter = 0;
					WholeBlink (50);
					BuildBoxes();
				}
				else if (Stage == 0)
				{
					NotMoving = false;
					BlinkCounter = 0;
					WholeBlink (50);
					try
					{
						this.getAppletContext().showDocument(new URL (this.getDocumentBase(),"top.htm"),"topwin");
					}
					catch (MalformedURLException m){}
				}
				else
				{
					NotMoving = false;
					BlinkCounter = 0;
					WholeBlink (50);
					try
					{
						this.getAppletContext().showDocument(new URL (this.getDocumentBase(),"bottom.htm"),"textwin");
					}
					catch (MalformedURLException m){}
				}
				Stage++;
			}
			if ((Stage > 2)&&(InsideBox (x, y)))
			{
				NotMoving = false;
				MoveBox();
				try
				{
					this.getAppletContext().showDocument(new URL (this.getDocumentBase(),"bottom"+WhichBox+".htm"),"textwin");
				}
				catch (MalformedURLException m){}
			}
		}
		return true;
	}

	public void UpdateScreen(int x, int y, int width, int height)
	{
		Graphics g = getGraphics();
		g.clipRect(x, y, width, height);
		paint(g);
		g.dispose();
	}

	public void paint (Graphics g)
	{
		g.drawImage (BackGround, 0, 0, this);
	}

	public void update (Graphics g)
	{
		paint(g);
	}
}