import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Box extends JApplet
  implements ActionListener
{
  private int xPos, yPos;

  /**
   *  Called automatically when the applet is initialized
   */
  public void init()
  {
    Container c = getContentPane();
    c.setBackground(Color.red);
    xPos = c.getWidth();
    yPos = c.getHeight() / 2;
    Timer clock = new Timer(30, this);  // Fires every 30 milliseconds 
    clock.start();
  }

  /**
   *  Called automatically after a repaint request
   */
  public void paint(Graphics g)
  {
    super.paint(g);
    g.setColor(Color.black);
    g.fillRect(xPos, yPos, 35, 20); 
  }

  /**
   *  Called automatically when the timer fires
   */
  public void actionPerformed(ActionEvent e)
  {
    Container c = getContentPane();
    xPos--;
    if (xPos < -100)
    {
      xPos = c.getWidth();
    }
    yPos = c.getHeight() / 2;
    repaint();
  }
}

