import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.*;

public class Eyes extends Applet implements Runnable, MouseListener, MouseMotionListener
{
	Thread runner;
    int lX, lY, rX, rY, lEyeX, rEyeX, lEyeY, rEyeY;
    boolean rPoked, lPoked, sleeping, painted;
    Image bg=null,buffer;
    Color  abscess, color;
    AudioClip kick=null;

	public void init()
	{
    String s1=getParameter("backimage");
    if(s1!=null && !s1.equals("NO"))
	    bg = getImage(getCodeBase(),s1);
    s1=getParameter("color");
    if(s1!=null && !s1.equals("NO"))
			color = new Color(Integer.parseInt(s1, 16));
    else
    	color=Color.white;
    	
    s1=getParameter("blackeye");
    if(s1!=null && !s1.equals("NO"))
			abscess = new Color(Integer.parseInt(s1, 16));
    else
    	abscess=Color.blue;
    s1=getParameter("kick");
    if(s1!=null && !s1.equals("NO"))
    	kick=getAudioClip(getCodeBase(),s1);
		try
		{
			lX=Integer.parseInt(getParameter("lX"));
		}
		catch(NumberFormatException e)
		{
			lX=40;
		}
		try
		{
			lY=Integer.parseInt(getParameter("lY"));
		}
		catch(NumberFormatException e)
		{
			lY=40;
		}
		try
		{
			rX=Integer.parseInt(getParameter("rX"));
		}
		catch(NumberFormatException e)
		{
			rX=70;
		}
		try
		{
			rY=Integer.parseInt(getParameter("rY"));
		}
		catch(NumberFormatException e)
		{
			rY=40;
		}
		buffer=createImage(getSize().width,getSize().height);
		addMouseListener(this);
		addMouseMotionListener(this);
	}

    public void start()
    {
        if(runner == null)
        {
            runner = new Thread(this);
            runner.start();
        }
    }

    public void stop()
    {
        if(runner != null)
        {
            runner.stop();
            runner = null;
        }
    }

    public void run()
    {
        do
            try
            {
                Thread.sleep(1000L);
            }
            catch(InterruptedException _ex) { }
        while(true);
    }

    public void paint(Graphics g)
    {
    	Graphics screen=null;
    	screen=g;
    	g=buffer.getGraphics();
    	
        	if(bg!=null)
        		g.drawImage(bg,0,0,this);
        if(!painted)
        {
        	if(bg!=null)
        		g.drawImage(bg,0,0,this);
        	else
        	{
        		g.setColor(Color.white);
        		g.fillRect(0, 0, getSize().width, getSize().height);
        	}
            painted = true;
        }
        if(lPoked && !sleeping)
            g.setColor(abscess);
        else
            g.setColor(color);
        g.fillOval(lX + 1, lY + 1, 19, 19);
        if(rPoked && !sleeping)
            g.setColor(abscess);
        else
            g.setColor(color);
        g.fillOval(rX + 1, rY + 1, 19, 19);
        g.setColor(Color.black);
        g.drawOval(lX, lY, 20, 20);
        g.drawOval(rX, rY, 20, 20);
        if(!sleeping && lEyeX >= 0)
        {
            g.fillOval(lEyeX, lEyeY, 6, 6);
            g.fillOval(rEyeX, rEyeY, 6, 6);
        } else
        {
            g.drawArc(rX, rY + 4, 20, 10, 0, -180);
            g.drawArc(lX, lY + 4, 20, 10, 0, -180);
        }
        screen.drawImage(buffer,0,0,this);

    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void mousePressed(MouseEvent event)
    {
    	int i=event.getX();
    	int j=event.getY();
        float f = i - lX - 10;
        float f1 = j - lY - 10;
        float f2 = (float)Math.sqrt(f * f + f1 * f1);
        
        
        if(f2 < 8F)
        {
            lPoked = true;
			      if(kick!=null)
							kick.play();
            repaint();
        }
        f = i - rX - 10;
        f1 = j - rY - 10;
        f2 = (float)Math.sqrt(f * f + f1 * f1);
        if(f2 < 8F)
        {
            rPoked = true;
		        if(kick!=null)
    		    	kick.play();
            repaint();
        }

    }

    public void mouseMoved(MouseEvent event)
    {
    	int i=event.getX();
    	int j=event.getY();
        float f = i - lX - 10;
        float f1 = j - lY - 10;
        float f2 = (float)Math.sqrt(f * f + f1 * f1);
        if(f2 > 7F)
        {
            f *= 7F / f2;
            f1 *= 7F / f2;
        }
        int k = (int)(((float)lX + f + 10F) - 2.0F);
        int l = (int)(((float)lY + f1 + 10F) - 2.0F);
        f = i - rX - 10;
        f1 = j - rY - 10;
        f2 = (float)Math.sqrt(f * f + f1 * f1);
        if(f2 > 7F)
        {
            f *= 7F / f2;
            f1 *= 7F / f2;
        }
        int i1 = (int)(((float)rX + f + 10F) - 2.0F);
        int j1 = (int)(((float)rY + f1 + 10F) - 2.0F);
        if(i1 != rEyeX || j1 != rEyeY || k != lEyeX || l != lEyeY)
        {
            rEyeX = i1;
            rEyeY = j1;
            lEyeX = k;
            lEyeY = l;
            repaint();
        }
    }

    public void mouseEntered(MouseEvent event)
    {
        sleeping = false;
        painted = false;
        repaint();
    }

    public void mouseExited(MouseEvent event)
    {
        sleeping = true;
        repaint();
        lPoked = false;
        rPoked = false;
    }

    public Eyes()
    {
        lEyeX = -1;
        rEyeX = -1;
        lEyeY = -1;
        rEyeY = -1;
        rPoked = false;
        lPoked = false;
        sleeping = false;
        painted = true;
    }
	public void mouseDragged(MouseEvent e){ }
	public void mouseReleased(MouseEvent e){ }
	public void mouseClicked(MouseEvent e){ }
}

