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

 public class BouncingBall extends Applet implements MouseListener
 {
  public final int NUM = 4;

  public Thread [] bounceBall = new Thread[NUM];
  public boolean xRight;
  public boolean yDown;
  public int  x;
  public int  y;
  public static int clicktime = 0;
  public Color [] color = {Color.blue, Color.red, Color.green, Color.magenta};
  public int i;
  
  public void init()
  {
   addMouseListener(this);
  }

  public void mouseClicked(MouseEvent e)
  {
   if(clicktime <NUM)
     {
      x=e.getX();
      y=e.getY();
      bounceBall[clicktime]=new Ball (this, x, y, clicktime);
      bounceBall[clicktime].start ();
      clicktime ++;
     }
  }

  public void paint(Graphics g)
  {
   g.setColor (Color.yellow);
   g.fillRect (0,0,315,215);

   drawThickRect(g);

  }

  public void drawThickRect(Graphics g)
  {
   g.setColor (Color.black);
   g.drawRect (0,0,315,215);
   g.drawRect (1,1,313,213);
   g.drawRect (2,2,311,211);
  }

     public void mouseExited(MouseEvent e){}
     public void mouseEntered(MouseEvent e){}
     public void mousePressed(MouseEvent e){}
     public void mouseReleased(MouseEvent e){} 
	 
    
  public class Ball extends Thread 
  {   
    Graphics g;
    int x, y, num;  
    boolean xRight, yDown;
    int sleepTime = 10;
    int bounces = 0;

    public Ball (Applet a, int x, int y, int num) 
    {
       xRight=true;
       yDown=true;
       g =  a.getGraphics();
       this.x = x;   
       this.y = y;    
       this.num = num;
    }  

    public void run()
    {
       while(true)
    {
       try
       {
          sleep(sleepTime);	 
       }	 
       catch (InterruptedException ie) { } 
       
       g.setColor (Color.yellow);
       g.fillOval (x,y,10,10);  
       drawThickRect(g);

       if(xRight)
           x++;
       else
           x--;
       if(yDown)
           y++;
       else
           y--;

       g.setColor (color[num] );
       g.fillOval (x,y,10,10);

       if(y<=0) throw new YLessException();
       else if(y>=205) throw new YGreatException();
       if(x<=0) throw new XLessException();
       else if(x>305) throw new XGreatException();

   } // eof while   	   
  } // eof run      

  public class YLessException extends ArithmeticException
  {
    public YLessException()
    {
	   super();
	   yDown = true;
           bounces++;
           if (bounces < 10) run();
           else
           {
            g.setColor (Color.yellow);
            g.fillOval (x,y+1,10,10);  
            drawThickRect(g);
           }
    }
  }
  public class YGreatException extends  ArithmeticException
  {
     public YGreatException()
     {
	   super();
	   yDown = false;
           bounces++;
           if (bounces < 10) run();
           else
           {
            g.setColor (Color.yellow);
            g.fillOval (x,y-1,10,10);  
            drawThickRect(g);
           }
     }
  }
  public class XLessException extends  ArithmeticException
  {
     public XLessException()
     {
	   super();
	   xRight = true;
           bounces++;
           if (bounces < 10) run();
           else
           {
            g.setColor (Color.yellow);
            g.fillOval (x+1,y,10,10);  
            drawThickRect(g);
           }
     }
  }
  public class XGreatException extends  ArithmeticException
  {
     public XGreatException()
     {
	   super();
	   xRight = false;
           bounces++;
           if (bounces < 10) run();
           else
           {
            g.setColor (Color.yellow);
            g.fillOval (x-1,y,10,10);  
            drawThickRect(g);
           }
     }
  }

  
  } // class Ball

} // class BouncingBall
