Bouncing ball

A simple java Applet program to create a bouncing ball

import java.applet.*;
import java.awt.*;
/* */
public class b_ball extends Applet implements Runnable
{
int y=0;
Thread t=null;
boolean flag,b=true,c=true;
public void init()
{
setBackground(Color.cyan);
}
public void start()
{
t=new Thread(this);
flag=false;
t.start();
}
public void run()
{
for( ; ;)
{
try
{
if(y<=500)
repaint();
Thread.sleep(2);
if(flag)
break;
}catch(InterruptedException e){}
}
}
public void stop()
{
flag=true;
t=null;
}
public void paint(Graphics g)
{
g.fillOval(10,y,40,40);
if(y==500)
{
b=false;c=true;}
if(!b)
y=y-1;
if(y==0){
c=false;b=true;}
if(!c)
y=y+1;
}
}
sample output:

No comments:

Post a Comment