//Title:        8. Hafta Odevi
//Version:      1.1
//Copyright:    Copyright (c) 1999
//Author:       Hasan Er
//Company:      Gebze Yuksek Teknoloji Enstitusu
//Description:
// version 1.1 Turhan Coban taraf¨ndan paint metodu
// de§istirilmistir.

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

public class H8O2 extends Applet implements ActionListener

{
    int bekleyen = 0;
    boolean kontrolEdenVar = false;
    IkiIsikKontroluThread yik[] = new IkiIsikKontroluThread[2];
    char which_light[]= new char[] {'r', 'r'};
    private static int x1=10;
    private static int y1= 40;
    private static int h1=360;
    private static int w1=100;

    private static int x2=210;
    private static int y2= 40;
    private static int h2=360;
    private static int w2=100;
    private static int radius=40;
    private static int hight_diff=30;
    TextField t1, t2;
    Label l1,l2,l3,l4;
    public void init() {
        t1=new TextField("12");
        t2=new TextField("12");
        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);
        yik[0]=new IkiIsikKontroluThread( this, 0);
        yik[0].start();
        yik[1]=new IkiIsikKontroluThread( this, 1);
        yik[1].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)
    {
        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[0]) {
            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,x1,y1);
      paintIsik(g,x2,y2);
    }

  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());
    }
  }
}
