Sunday, November 4, 2012

Moving ball across the boundry

 import java.applet.*;   
 import java.awt.Color;  
 import java.awt.Graphics;  
 import java.util.logging.Level;  
 import java.util.logging.Logger;  
 public class Moveball extends Applet implements Runnable{  
   public int x=1,y=0;  
   public void init(){  
     Thread t=new Thread(this);  
     t.start();  
   }  
   public void run(){  
     while(true){  
       try {  
        if(x<300 && y==0)x=x+1;  
        if(x==300 && y<150)y++;  
        if(y==150 && x>0)x=x-1;  
        if(x==0 && y<=150)y=y-1;  
        Thread.sleep(3);  
        repaint();  
        } catch (InterruptedException ex) {  
           Logger.getLogger(Moveball.class.getName()).log(Level.SEVERE, null, ex);  
         }  
     }  
   }  
   public void paint(Graphics g){  
       setBackground(Color.BLUE);  
       setForeground(Color.GREEN);  
       g.drawOval(x, y, 50, 50);  
   }    
 }  

No comments:

Post a Comment