Sunday 6 January 2013

A moving vehicle with applet in java


package animation1;
import java.applet.Applet;
import java.awt.*;

public class Animation1 extends Applet implements Runnable{
  private int x,y,x1,y1,x3,y3;
  Thread th;
    public void init()
   {  x=50;
   y=50;
   x1=50;
   y1=200;
   x3=150;
   y3=200;
  
       setForeground(Color.BLACK);
     
           //repaint();
          
       
     
   }
    public void start()
    {
        th=new Thread (this);
        th.start();
       
    }
   public void run()
   {
        for(int i=0;i<1000;i++)
       { 
           x=x+3;
          // y++;
           x1=x1+3;
          // y1++;
           x3=x3+3;
          // y3++;
            repaint();
           try{
              
         Thread.sleep(50);
              
           }
          catch(InterruptedException e){}
       }
   }
  /* public void update(Graphics g)
    {
        paint(g);
    }*/
   public void paint(Graphics g)
   {  
       g.drawRect(x,y,150,150);
       g.drawOval(x1,y1,50,50);
       g.drawOval(x3,y3,50,50);
       
             
   
            }
}

No comments:

Post a Comment