import java.applet.*;
import java.awt.*;
import java.util.*;
import java.lang.*;

public class Bounce extends Applet implements Runnable
{
   Thread runner = null;
   int x=50, y=50, xinc=4, yinc=5;
   boolean clear_screen=true;
   int color_change = 0;
   Color ball_color;
        

public void init()
{
        ball_color = new Color (getRand(), getRand(), getRand());

}

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() {

  this.getGraphics().clearRect(0, 0, this.size().width, this.size().height);
  this.getGraphics().fillOval (x, y, 10, 10);   //initialize to black background
  while (runner != null) {
    if ((x >= this.size().width) || (x <= 0))
       {
       xinc = -xinc;
       ball_color = new Color (getRand(), getRand(), getRand());

       }
    if ((y >= this.size().height) || (y <= 0))
       {
       yinc = -yinc;
       ball_color = new Color (getRand(), getRand(), getRand());
       }

    try {Thread.sleep(50);} catch (InterruptedException e){}
    repaint();
  }
  runner = null;
}
   private int getRand()
   {
   return (int)(Math.random() * 255);
   }

   public void update(Graphics g)
   {
      paint(g);
   }

   public void paint(Graphics g)
   {
      g.setColor(getBackground());

      g.fillOval (x, y, 10, 10);
      y+= yinc;
      x+= xinc;
      color_change = 0;
      g.setColor(ball_color );
      g.fillOval (x, y, 10, 10);
   }

}
