import java.awt.*; 
import java.applet.*;
import java.awt.event.*;
import java.applet.*;
public class IkiTrafikIsigi extends Applet implements ActionListener
{
    int bekleyen = 0;
    boolean kontrolEdenVar = false;
    IkiIsikKontroluThread yik[] = new IkiIsikKontroluThread[2];
    char which_light[]= new char[] {'r', 'r'};
    TextField t1, t2;
    Label l1,l2,l3,l4;
    public void init()
    {
        t1=new TextField("4");
        t2=new TextField("5");
        l1=new Label("kirmizi isik yanma zamani : ");
        l2=new Label("saniye");
        l3=new Label("yesil isik yanma zamani : ");
        l4=new Label("saniye");
        add(l1);
        add(t1);
        add(l2);
        add(l3);
        add(t2);
        add(l4);
        t1.addActionListener(this);
        t2.addActionListener(this);
        for(int i=0;i<2;i++)
        {
        yik[i]=new IkiIsikKontroluThread( this, i);
        yik[i].start();
        }
    }
    public synchronized void kontrolAl()
    {
      try
      {
        if ( kontrolEdenVar )
        {
          bekleyen++;
          wait();
        }
        else
        {
          kontrolEdenVar = true;
        }
      }
      catch ( InterruptedException interruptedEx)
            { interruptedEx.printStackTrace();}
    }
    public synchronized void kontrolBirak()
    {
      if ( bekleyen>0 )
      {
        notify();
        bekleyen--;
      }
      else
      {
        kontrolEdenVar = false; 
      } 
    } 
    public void paintIsik(Graphics g,int xi,int yi,int c) 
    { 
        int h1=360; 
        int w1=100; 
        int radius=40; 
        int hight_diff=30; 
        g.setColor(Color.black); 
        g.fillRect(xi,yi,w1,h1); 
        g.setColor(Color.white); 
        g.drawRect(xi-1,yi-1,w1+2,h1+2); 
        g.drawRect(xi-2,yi-2,w1+4,h1+4); 
        switch(which_light[c]) 
        { 
            case 'g': 
                g.setColor(Color.lightGray); 
                g.fillOval(xi+10,yi+hight_diff,radius*2,radius*2); 
                g.fillOval(xi+10,yi+hight_diff*2+radius*2,radius*2,radius*2); 
                g.setColor(Color.green); 
                g.fillOval(xi+10,yi+hight_diff*3+radius*4,radius*2,radius*2); 
                break; 
            case 'r': 
                g.setColor(Color.lightGray); 
                g.fillOval(xi+10,yi+hight_diff*2+radius*2,radius*2,radius*2); 
                g.fillOval(xi+10,yi+hight_diff*3+radius*4,radius*2,radius*2); 
                g.setColor(Color.red); 
                g.fillOval(xi+10,yi+hight_diff,radius*2,radius*2); 
                break; 
            case 'y': 
                g.setColor(Color.lightGray); 
                g.fillOval(xi+10,yi+hight_diff,radius*2,radius*2); 
                g.fillOval(xi+10,yi+hight_diff*3+radius*4,radius*2,radius*2); 
                g.setColor(Color.yellow); 
                g.fillOval(xi+10,yi+hight_diff*2+radius*2,radius*2,radius*2); 
                break; 
        } 
    } 
    public void paint(Graphics g) 
    { 
      paintIsik(g,10,140,0);
      paintIsik(g,210,140,1); 
    } 
  public void actionPerformed(ActionEvent e) 
  { 
    if(e.getSource()==t1) 
    { 
       Double i1=new Double(t1.getText()); 
       for ( int i=0; i<yik.length; i++) 
         yik[i].setKirmiziZaman(i1.doubleValue()); 
    } 
    else if(e.getSource()==t2) 
    { 
       Double i1=new Double(t2.getText()); 
       for ( int i=0; i<yik.length; i++) 
         yik[i].setYesilZaman(i1.doubleValue()); 
    } 
  } 
}
