Monday, March 7, 2011

Traffic Example in swing

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

public class Traffic extends Applet implements Runnable {

  
    static int i = 0;
    Thread t1, t2, t3;

    public void paint(Graphics g)
         {
        g.drawOval(50,350,30,30);
        g.drawOval(50,300,30,30);
        g.drawOval(50,250,30,30);
        
                if(i == 0)
        {
                        g.setColor(Color.RED);
                        g.fillOval(50,250,30,30);
                        try{
                        t1.sleep(5000);
                        }catch(Exception e){}
            repaint();
            i=1;
                }   else if(i==1)
        {
                        g.setColor(Color.YELLOW);
            g.fillOval(50,300,30,30);
                        try{
                        t2.sleep(1000);
                        }catch(Exception e){}
            repaint();
            i=2;
                }   else if(i==2)
        {
                        g.setColor(Color.GREEN);
                        g.fillOval(50,350,30,30);
                        try{
                        t3.sleep(4000);
                        }catch(Exception e){}
            repaint();
            i=0;
               }
       
         }

    @Override
    public void run() {
        // TODO Auto-generated method stub
       
    }
}
/*
 * <APPLET CODE="Traffic" height=700 width=500> </APPLET>
 */

0 comments:

Post a Comment