import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import javax.swing.JApplet;      // wird jetzt neu verwendet

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/* TrafficLight wird jetzt Unterklasse
   von javax.swing.JApplet
   statt javax.swing.JFrame */
   
public class TrafficLight extends JApplet
{ /*  The Traffic Light program by J M Bishop Oct 1997 
   *  revised by U Kastens Jan 1999, M Thies Apr 2005 */

  // Object variables used by Listeners:
  private String [] message = // Duration of each light
      {"default","default","default","default"};
  private int light = 0; // The chosen light
  private LightsCanvas lights; // Contains the picture of the lights
  private JTextField duration;
  private JComboBox colours;

  /* Anweisungen aus dem Konstruktor der Unterklasse zu Frame
     gehören in die init-Methode der Unterklasse zu Applet. */

  public void init () 
  { // Construction of the root JApplet
    Container content = getContentPane();
    content.setLayout(new BorderLayout()); // The layout for the root JApplet

    JPanel title = new JPanel(new FlowLayout(FlowLayout.CENTER));  // The title is centered
    title.add(new JLabel("Savanna Traffic Light Simulation"));
    content.add(title, BorderLayout.NORTH);

    // The lights are painted as defined in a separate class
    lights = new LightsCanvas(message);
    content.add(lights, BorderLayout.CENTER);

    // Choice of a light
    colours = new JComboBox(new String[] { "Red", "Yellow", "Green", "Walk" });

    colours.addItemListener(
          new ItemListener()
          {
             public void itemStateChanged(ItemEvent e)
             {
                String s = e.getItem().toString();
                if (s.equals("Red")) light = 0;
                else if (s.equals("Yellow")) light = 1;
                else if (s.equals("Green")) light = 2;
                else if (s.equals("Walk")) light = 3;
             }
          }
    );

    // Field for input of the duration of the chosen light:
    JPanel labeledText = new JPanel(new FlowLayout(FlowLayout.LEFT));
    labeledText.add(new JLabel("Duration"));
    duration = new JTextField(3);
    duration.setEditable(true);

    duration.addActionListener(
          new ActionListener()
          {
             public void actionPerformed(ActionEvent e)
             {
                // message[light] = ((JTextField) e.getSource()).getText();
                message[light] = e.getActionCommand();
                lights.repaint();
             }
          }
    );
    labeledText.add(duration);

    // Two Buttons:
    JButton walkButton = new JButton("Walk"); // no action yet

   /* Close-Button entfällt, da ein Applet nicht geschlossen
      werden kann. Nur beim Verlassen der HTML-Seite mit dem
      Applet verschwindet das Applet natürlich. */

    // Compose the button line:
    JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add(colours);
    buttons.add(labeledText);
    buttons.add(walkButton);
    // kein Close-Button mehr (war vorher Komponente in diesem Panel)

    content.add(buttons, BorderLayout.SOUTH);

    /* Eigenschaften des Fenster einstellen entfällt:
       Größe wird im Applet-Element der HTML-Seite bestimmt,
       Verhalten für Schließknopf gibt es nicht beim Applet
       und Sichtbar wird das Applet automatisch vom Browser
       gemacht. */
  }

/* main-Methode entfällt, da nur JFrame-Objekt erzeugt wurde. */

}

/* Ampelsteuerung verwendet spezielle Unterklasse von JComponent.
   Steht der Übersichtlichkeit halber auch in dieser Datei.
   Das ist nur möglich, weil die Klasse LightsCanvas nicht als
   public deklariert ist. */

class LightsCanvas extends JComponent
{
   private String[] msg;

   LightsCanvas (String[] m)
   { msg = m; }

   public void paint (Graphics g)
   {
      super.paint(g);
      g.drawOval(87, 10, 30, 68);
      g.setColor(Color.red);
      g.fillOval(95, 15, 15, 15);
      g.setColor(Color.yellow);
      g.fillOval(95, 35, 15, 15);
      g.setColor(Color.green);
      g.fillOval(95, 55, 15, 15);
      // walk light is also green
      g.fillOval(95, 85, 15, 15);
      g.setColor(Color.black);
      g.drawString("RED", 15 ,28);
      g.drawString("YELLOW", 15, 48);
      g.drawString("GREEN", 15, 68);
      g.drawString("WALK", 15, 98);
      g.drawString(msg[0], 135 ,28);
      g.drawString(msg[1], 135, 48);
      g.drawString(msg[2], 135, 68);
      g.drawString(msg[3], 135, 98);
   }
}



