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

/* A very simple applet that demonstrates the simplest animation techniques.
No attempt is made here to reduce or eliminate flicker. The animation just
sends a white rectangle back and forth (horizontally) across a canvas.
public class AnimJava extends Applet implements ActionListener
   Button start, stop; // stop and start the animation
   xCanvas can; // custom canvas on which to draw
   Panel controls; // holds the buttons

public void init()
   start = new Button("Start");
   stop = new Button("Stop");

   controls = new Panel();
   controls.setLayout ( new GridLayout(1,2));

   can = new xCanvas();

   this.setLayout ( new BorderLayout());


   add(controls, BorderLayout.NORTH);
   add(can, BorderLayout.CENTER);

} // end of init

//Respond to button clicks by calling methods in the custom
//canvas to start and stop the animation

public void actionPerformed (ActionEvent e)
   if (e.getSource() == start)

   if (e.getSource() == stop)
} // end applet class

//Custom canvas class implements runnable so must have a run method

class xCanvas extends Canvas implements Runnable
   Thread runner = null;
   int x = 30; //hard coding opening coordinates of rectangle
   int y = 100;
   int w = 20;
   int incr = 4;

   boolean black = true; //for controlling color
   boolean keepDrawing; //for controlling run loop
//Constructor does nothing special
public xCanvas()
//Called by parent applet, this starts the thread's code

public void startAnim()
   if (runner == null)
     runner = new Thread(this);
     keepDrawing = true;

//Called by parent applet, this stops the thread's code
public void stopAnim()
   if (runner != null)
      keepDrawing = false;
      runner = null;

//The thread's code runs here

public void run()
   Dimension d = getSize();

   while (keepDrawing)
      if ((x + w) > d.width ) //if rectangle at right border
      incr = -incr; //change direction

      if (x <= 0) //if rectangle at left border
      incr = -incr; //change direction

      x+= incr; //add to current x coordinate

      repaint(); //call paint

        runner.sleep(10); //sleep a little to slow it down
     catch (InterruptedException e) {}

} // end of run


public void paint(Graphics g)
   if (black) //toggle current drawing color
     black = !black;
     g.fillRect(x, y, w, w);
} // end paint

} // end of class