import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class SensoApplet extends Applet
	{
	final int ROT=0,GELB=1,LILA=2,ORANGE=3,START=4; //die farben
	final int Kx[]={64,131,196,131,131};		//die x-koordinaten und
	final int Ky[]={159,225,159,95,159};		//die y-koordinaten der tastenmittelpunkte
	final int R=32,R1=22;				   //die radien der tasten
	int folge[] = new int[20];
	int z, anzahl;
	String kommentar= new String("");
	boolean vorgespielt, nachgespielt, verloren;
	Image bild;
	AudioClip ton[] = new AudioClip[6];
	Graphics temp;
	
	private class MyMouse extends MouseAdapter
		{
		public void mousePressed(MouseEvent e)
			{
			int x=e.getX();
			int y=e.getY();
			int farbe=4;
			if ((Kx[ROT]   -x)*(Kx[ROT]   -x) + (Ky[ROT]   -y)*(Ky[ROT]   -y) <= R*R) farbe=ROT;	 //Kreisformel: 
			if ((Kx[GELB]  -x)*(Kx[GELB]  -x) + (Ky[GELB]  -y)*(Ky[GELB]  -y) <= R*R) farbe=GELB;	//x*x + y*y = r*r
			if ((Kx[LILA]  -x)*(Kx[LILA]  -x) + (Ky[LILA]  -y)*(Ky[LILA]  -y) <= R*R) farbe=LILA;	//mit Translation
			if ((Kx[ORANGE]-x)*(Kx[ORANGE]-x) + (Ky[ORANGE]-y)*(Ky[ORANGE]-y) <= R*R) farbe=ORANGE;
			if (x>290 && x<359 && y>243 && y<259) neuesSpiel();
			if (verloren==true) return;
			if ((Kx[START]-x)*(Kx[START]-x) + (Ky[START]-y)*(Ky[START]-y) <= R1*R1 && vorgespielt==false) vorspielen();
			if (farbe!=4 && nachgespielt==false) nachspielen(farbe);
			}
		}	
		
	public void init()
		{
    	for (int i=0;i<=5;i++)
    		{
    		ton[i] = getAudioClip(getCodeBase(),(i+".au"));
    		}
    	bild= getImage(getCodeBase(),"senso.gif");
    	temp = getGraphics();
		addMouseListener(new MyMouse());
		neuesSpiel();
		setBackground(Color.white);
		}
		
	public void paint(Graphics g)
		{
		g.drawImage(bild,0,0,this);
		//g.drawString(kommentar,258,200);
		schreiben(kommentar);
		}

	private void neuesSpiel()
		{
		anzahl=3;
		verloren=false;
		vorgespielt=false;
		nachgespielt=true;
		kommentar="Wir beginnen bei 3 Tönen";
		repaint();
		for(int i=0;i<20;i++)
			{
			folge[i]=(int)(Math.random()*4);
			}
		}
				
	private void vorspielen()
		{	
		for(int i=0;i<anzahl;i++)
			{
			blinken(folge[i]);
			pause(100);
			}
		vorgespielt=true;
		nachgespielt=false;
		z=0;
		}	
				
	private void nachspielen(int farbe)
		{	
		blinken(farbe);
		if (farbe!=folge[z]) //abbrechen
			{
			ton[4].play();
			kommentar="Sorry, nicht geschafft!";
			z=0;
			verloren=true;
			}
		z++;
		if (z==20)
			{
			ton[5].play();
			kommentar="Wow, 20 Töne. Geschafft!";
			verloren=true;
			z=0;
			}	
		if (z==anzahl)
			{
			kommentar=("Bis jetzt hast du "+anzahl+" Töne");
			z=0;
			anzahl++;			
			vorgespielt=false;
			nachgespielt=true;
			}	
			schreiben(kommentar);
		}	
		
	 private void blinken(int farbe)
		{
		temp.setColor(Color.white);
		temp.fillRect((Kx[farbe]-36),(Ky[farbe]-36),72,72);
		temp.setColor(Color.black);
		ton[farbe].play();
		pause(400);
		temp.drawImage(bild,0,0,this);
		schreiben(kommentar);
		}
	
	private void pause(int zeit)
		{
		long startzeit=System.currentTimeMillis();
		while ((System.currentTimeMillis() - startzeit) <=zeit) {}
		}	
		
	private void schreiben(String kommentar)
		{
		temp.setColor(Color.white);
		temp.fillRect(258,200,150,30);
		temp.setColor(Color.black);
		temp.drawString(kommentar,258,220);
		}		
	}