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

public class JavaZes extends Applet
                     implements Runnable
{
  Thread beweeg;
  int    status = 0;
         /* 0 = alles uit
          * 1 = rood
          * 2 = oranje
          * 3 = groen
          */
  Color lamp1aan = Color.red;
  Color lamp2aan = Color.orange;
  Color lamp3aan = Color.green;
  Color lamp1uit = new Color(139, 0, 0);
  Color lamp2uit = new Color(128, 128, 0);
  Color lamp3uit = new Color(107, 142, 35);

  public void init()
  {
    setBackground(Color.blue);
    beweeg = new Thread(this);
    beweeg.start();
  }

  public void paint(Graphics pen)
  {
    tekenStoplicht(pen, 200, 10, 100, 480);
  }

  private void tekenStoplicht(Graphics pen, int x, int y, int breedte, int hoogte)
  {
    double multiBreedte    = (double) breedte / 5.0;
    double multiHoogte     = (double) hoogte  / 24.0;
    int    lichtBreedte    = (int) (multiBreedte * 4.0);
    int    lichtHoogte     = (int) (multiHoogte  * 4.0);
    int    stoplichtHoogte = (int) (multiHoogte  * 14.0);

    pen.setColor(Color.black);
    pen.fillRoundRect(
      x,
      y,
      breedte,
      stoplichtHoogte,
      (int) multiBreedte,
      (int) multiHoogte
    );

    if (status == 1)
      pen.setColor(lamp1aan);
    else
      pen.setColor(lamp1uit);
    pen.fillOval(
      (int) ((double) x + multiBreedte / 2.0),
      (int) ((double) y + multiHoogte  / 2.0),
      lichtBreedte,
      lichtHoogte
    );

    if (status == 2)
      pen.setColor(lamp2aan);
    else
      pen.setColor(lamp2uit);
    pen.fillOval(
      (int) ((double) x + multiBreedte / 2.0),
      (int) ((double) y + multiHoogte * 5.5 - multiHoogte / 2.0),
      lichtBreedte,
      lichtHoogte
    );

    if (status == 3)
      pen.setColor(lamp3aan);
    else
      pen.setColor(lamp3uit);
    pen.fillOval(
      (int) ((double) x + multiBreedte / 2.0),
      (int) ((double) y + multiHoogte * 10.0 - multiHoogte / 2.0),
      lichtBreedte,
      lichtHoogte
    );

    tekenPaal(
      pen,
      (int) ((double) x + multiBreedte * 2.0),
      y + stoplichtHoogte,
      (int) multiBreedte,
      (int) multiHoogte,
      9
    );
  }

  private void tekenPaal(Graphics pen, int x, int y, int blokBreedte, int blokHoogte, int aantalBlokjes)
  {
    boolean switcher = false;
    for (int i = 0; i < aantalBlokjes; i++)
    {
      switcher = !switcher;
      if (switcher)
        pen.setColor(Color.black);
      else
        pen.setColor(Color.white);
      pen.fillRect(x, y + i * blokHoogte, blokBreedte, blokHoogte);
    }
  }

  private void switchLight()
  {
    status++;
    if (status == 4)
      status = 1;
    repaint();
  }

  public void run()
  {
    while (beweeg != null)
    {
      try
      {
        beweeg.sleep(1000);
      }
      catch (Exception e)
      {
      }
      switchLight();
    }
  }
}
