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

class isikKontroluThread extends Thread
{
Applet a;
public double yesilZaman;
public double kirmiziZaman;
public char hangi_isik;

   public isikKontroluThread(Applet ai)
   {
   a=ai;
   yesilZaman=12.0;
   kirmiziZaman=4.0;
   hangi_isik='y';
   }

   public void run()
   {
   while(true)
   {
    a.repaint();
     
    try {
        if(hangi_isik=='r')
           {
           Thread.sleep((int)(kirmiziZaman*1000));
           }
        }
    catch(InterruptedException e)
    {System.err.println(e.toString());}

    try {
        if(hangi_isik=='r')
           {
           Thread.sleep((int)(yesilZaman*1000));
           }
        }
    catch(InterruptedException e)
    {System.err.println(e.toString());}

   }
   }

   public void setYesilZaman(double zi)
   {
     yesilZaman=zi;
   }

   public void setKirmiziZaman(double zi)
   {
     kirmiziZaman=zi;
   }

   public double getYesilZaman()
   {
     return yesilZaman;
   }

   public double getKirmiziZaman()
   {
     return kirmiziZaman;
   }

   public char getIsik()
   {
     return hangi_isik;
   }

   public void setIsik(char hi)
   {
     hangi_isik=hi;
   }

} 

public class H8O1 extends Applet implements ActionListener
{
    isikKontroluThread yik;
    private Button light_switch;
    private static int x=10;
    private static int y= 40;
    private static int h=360;
    private static int w=100;
    private static int radius=40;
    private static int hight_diff=30;
    TextField t1;
    TextField t2;
    Label l1,l2,l3,l4;
    public void init()
    {
        yik=new isikKontroluThread(this);
        yik.start();
        light_switch = new Button("Switch");
        light_switch.setForeground(Color.black);
        light_switch.setBackground(Color.lightGray);
        t1=new TextField(""+yik.getYesilZaman());
        t2=new TextField(""+yik.getKirmiziZaman());
        l1=new Label("yesil isik yanma zamani : ");
        l2=new Label("saniye");
        l3=new Label("kirmizi isik yanma zamani : ");
        l4=new Label("saniye");
        add(light_switch);
        add(l1);
        add(t1);
        add(l2);
        add(l3);
        add(t2);
        add(l4);
        light_switch.addActionListener(this);
        t1.addActionListener(this);
        t2.addActionListener(this);
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(x,y,w,h);
        g.setColor(Color.white);
        g.drawRect(x-1,y-1,w+2,h+2);
        g.drawRect(x-2,y-2,w+4,h+4);
        switch(yik.getIsik()) {
            case 'r':
                yik.setIsik('g');
                g.setColor(Color.lightGray);
                g.fillOval(x+10,y+hight_diff,radius*2,radius*2);
                g.fillOval(x+10,y+hight_diff*2+radius*2,radius*2,radius*2);
                g.setColor(Color.green);
                g.fillOval(x+10,y+hight_diff*3+radius*4,radius*2,radius*2);
                break;
            case 'y':
                yik.setIsik('r');                
                g.setColor(Color.lightGray);
                g.fillOval(x+10,y+hight_diff*2+radius*2,radius*2,radius*2);
                g.fillOval(x+10,y+hight_diff*3+radius*4,radius*2,radius*2);
                g.setColor(Color.red);
                g.fillOval(x+10,y+hight_diff,radius*2,radius*2);
                break;
            case 'g':
                yik.setIsik('y');  
                g.setColor(Color.lightGray);
                g.fillOval(x+10,y+hight_diff,radius*2,radius*2);
                g.fillOval(x+10,y+hight_diff*3+radius*4,radius*2,radius*2);
                g.setColor(Color.yellow);
                g.fillOval(x+10,y+hight_diff*2+radius*2,radius*2,radius*2);
                repaint(1000);
        }
    }

public void actionPerformed(ActionEvent e)
{
  if(e.getSource()==light_switch)
  {           
     repaint();
     try {Thread.sleep(1000);} catch (Exception exc) {}
  }
  else if(e.getSource()==t1)
  {
     Double i1=new Double(t1.getText());
     yik.setYesilZaman(i1.doubleValue());
  }
  else if(e.getSource()==t2)
  {
     Double i1=new Double(t1.getText());
     yik.setKirmiziZaman(i1.doubleValue());
  }
}
}
