Search Here

Java program for creating Bouncing Balls


/* Bouncing Ball using applet */

import java.awt.*;
import java.applet.*;
/* 
 <applet code=bouncing_ball width=1200 height=600>
 </applet>
*/

public class bouncing_ball extends Applet
{
 public void init()
 {
  setBackground(Color.white);
 }
 public void paint(Graphics g)
 {
  while(true)
  {
   for(int i=80;i<=520;i++)
   {
    g.setColor(Color.red);
    g.fillOval(i,i,80,80);
    g.setColor(Color.blue);
    g.fillOval(1120-i,i,80,80); 
    try
    {
     Thread.sleep(10);
    }
    catch(Exception e)
    {}
    g.setColor(Color.white);
    g.fillOval(i,i,80,80);
    g.setColor(Color.white);
    g.fillOval(1120-i,i,80,80);
    
   }
   for(int i=520;i>=80;i--)
   {
    g.setColor(Color.red);
    g.fillOval(520+(520-i),i,80,80);
    g.setColor(Color.blue);
    g.fillOval(i,i,80,80);
    try
    {
     Thread.sleep(10);
    }
    catch(Exception e)
    {}
    g.setColor(Color.white);
    g.fillOval(520+(520-i),i,80,80);
     g.setColor(Color.white);
    g.fillOval(i,i,80,80);
   }
  }  
 }
}

No comments:

Post a Comment