How to make a bouncing ball animation in java

This is a very simple program exhibiting basic animation of a bouncing ball. The ball will bounce 5 times and will finally stop.

The ball will move downwards at first and will undergo an impact with the baseline. There would be a distortion in the shape of the ball due to  contact with the ground. After the ball will bounce back upwards and the same process will go on till the ball stops.

This program is created using Applet Class in Java. This is a very basic animation and is very useful for beginners.

The code for the whole program is below :

import java.awt.*;

import java.applet.*;
public class ball extends Applet  {

  public void init()
  {
    resize(500,500);
  }
  public void paint(Graphics g)
  {
    Font f=new Font("Chiller",Font.BOLD,54);
    Dimension d= getSize();
    
    //fillOval will be used as the ball
    g.setColor(Color.red);
    g.fillOval(100, 100, 30, 30);
  g.setFont(f);
        g.setColor(Color.black);
  g.fillRect(0, 430, 500, 450);
  //Caption at the start of applet for 2 secs
        g.setColor(Color.blue);
      g.drawString("Bouncing Ball", 200, 300);
    try{Thread.sleep(2000);}catch(Exception e){}
    
    //Infinite bounce   
    
        int i=10,c=0,x=100,y=100;
        while(y<400)
      {
          //ball falling
          try{
            g.setColor(Color.black);
            g.fillRect(0, 430, 500, 450);
      
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(40);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y+=i;
        x++;
        c++;
      }
      catch(Exception e)
      {}
      }g.setColor(Color.black);
      g.fillRect(0, 430, 500, 450);
      
      
      //distortion in the shape of ball due to impact on contact with the ground
      g.setColor(Color.red);
      g.fillOval(x, y, 40, 28);
      c=0;
      
      while(y>200)
      { 
        //ball bouncing back from the ground upwards
        
        try{g.setColor(Color.black);g.fillRect(0, 430, 500, 450);
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(30);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y-=i;
        x++;
        c++;
      }catch(Exception e){}}
      g.setColor(Color.black);
      g.fillRect(0, 430, 500, 450);
      
      //ball at the extreme top
      g.setColor(Color.red);
      g.fillOval(x, y, 30, 31);
      c=0;
      try{Thread.sleep(20);}
      catch(Exception e){}
      //in air
      for(int p=0;p<5;p++)
      {
        try{
          
          g.fillOval(x, y, 30, 30);
         Thread.sleep(5);
          g.setColor(Color.white);
          g.fillRect(0,0,d.width,d.height);
           g.setColor(Color.black);
          g.fillRect(0, 430, 500, 450);
          x++;
        }
        catch(Exception e)
        {}
      }
      
      while(y<401)
      {
          //ball falling
          try{
            g.setColor(Color.black);
            g.fillRect(0, 430, 500, 450);
      
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(40);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y+=i;
        x++;
        c++;
      }
      catch(Exception e)
      {}
      }g.setColor(Color.black);
      g.fillRect(0, 430, 500, 450);
      g.setColor(Color.red);
      
      //distortion in the shape of ball due to impact on contact with the ground
      
      g.fillOval(x, y, 40, 28);
      c=0;
      while(y>300)
      { 
        // 2ball bouncing back from the ground upwards
        
        try{g.setColor(Color.black);g.fillRect(0, 430, 500, 450);
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(30);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y-=i;
        x++;
        c++;
      }catch(Exception e){}}
      g.setColor(Color.black);
      g.fillRect(0, 430, 500, 450);
      
      //ball at the extreme top
      g.setColor(Color.red);
      g.fillOval(x, y, 30, 31);
      c=0;
      try{Thread.sleep(30);}
      catch(Exception e){}
      //in air
      for(int p=0;p<5;p++)
      {
        try{
          
          g.fillOval(x, y, 30, 30);
           Thread.sleep(5);
          g.setColor(Color.white);
          g.fillRect(0,0,d.width,d.height);
          g.setColor(Color.black);
          g.fillRect(0, 430, 500, 450);
          x++;
        }
        catch(Exception e)
        {}
      }
      while(y<400)
      {
          //ball falling
          try{
            g.setColor(Color.black);
            g.fillRect(0, 430, 500, 450);
      
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(40);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y+=i;
        x++;
        c++;
      }
      catch(Exception e)
      {}
      }g.setColor(Color.black);
      g.fillRect(0, 430, 500, 450);
      g.setColor(Color.red);
      g.fillOval(x, y, 40, 28);
      c=0;
      
      while(y>350)
      { 
        // 3ball bouncing back from the ground upwards
        
        try{g.setColor(Color.black);g.fillRect(0, 430, 500, 450);
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(30);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y-=i;
        x++;
        c++;
      }catch(Exception e){}}
      g.setColor(Color.black);
      g.fillRect(0, 430, 500, 450);
      
      //ball at the extreme top
      g.setColor(Color.red);
      g.fillOval(x, y, 30, 31);
      c=0;
      try{Thread.sleep(30);}
      catch(Exception e){}
      //in air
      for(int p=0;p<3;p++)
      {
        try{
          
          g.fillOval(x, y, 30, 30);Thread.sleep(5);g.setColor(Color.white);
          g.fillRect(0,0,d.width,d.height);g.setColor(Color.black);g.fillRect(0, 430, 500, 450);
          x++;
        }
        catch(Exception e)
        {}
      }
      while(y<400)
      {
          //ball falling
          try{
            g.setColor(Color.black);
            g.fillRect(0, 430, 500, 450);
      
        g.setColor(Color.red);
        g.fillOval(x,y,30,30);
        Thread.sleep(40);
        g.setColor(Color.white);
        g.fillRect(0,0,d.width,d.height);
        y+=i;
        x++;
        c++;
      }
          catch(Exception e)
          {}
          }g.setColor(Color.black);
          g.fillRect(0, 430, 500, 450);
          g.setColor(Color.red);
          g.fillOval(x, y, 40, 28);
          c=0;
          while(y>380)
          { 
            // 3ball bouncing back from the ground upwards
            
            try{g.setColor(Color.black);g.fillRect(0, 430, 500, 450);
            g.setColor(Color.red);
            g.fillOval(x,y,30,30);
            Thread.sleep(30);
            g.setColor(Color.white);
            g.fillRect(0,0,d.width,d.height);
            y-=i;
            x++;
            c++;
          }catch(Exception e){}}
          g.setColor(Color.black);
          g.fillRect(0, 430, 500, 450);
          
          //ball at the extreme top
          g.setColor(Color.red);
          g.fillOval(x, y, 30, 31);
          c=0;
          try{Thread.sleep(30);}
          catch(Exception e){}
          //in air
          for(int p=0;p<3;p++)
          {
            try{
              
              g.fillOval(x, y, 30, 30);
              Thread.sleep(5);
              g.setColor(Color.white);
              g.fillRect(0,0,d.width,d.height);
              g.setColor(Color.black);
              g.fillRect(0, 430, 500, 450);
              x++;
            }
            catch(Exception e)
            {}
          }
          while(y<400)
          {
              //ball falling
              try{
                g.setColor(Color.black);
                g.fillRect(0, 430, 500, 450);
          
            g.setColor(Color.red);
            g.fillOval(x,y,30,30);
            Thread.sleep(40);
            g.setColor(Color.white);
            g.fillRect(0,0,d.width,d.height);
            y+=i;
            x++;
            c++;
          }
              catch(Exception e)
              {}
              }g.setColor(Color.black);
              g.fillRect(0, 430, 500, 450);
              g.setColor(Color.red);
              g.fillOval(x, y, 40, 28);
              try{Thread.sleep(10);}
              catch(Exception e){}
              g.setColor(Color.white);
              g.fillRect(0, 0, d.width, d.height);
              g.setColor(Color.red);
              g.fillOval(x, y, 30, 30);
              g.setColor(Color.black);
              g.fillRect(0, 430, 500, 450);
              c=0;
  }
    
  
}

This program has 3 basic steps :

1.Falling of ball
2.Distortion in the shape of ball
3.Upward movement of ball
4.Forward movement of ball

Lets take a look on these steps with reference to the code :

1.Falling of ball

while(y<400)
    {
        //ball falling
        try{
          g.setColor(Color.black);
          g.fillRect(0, 430, 500, 450);
    
      g.setColor(Color.red);
      g.fillOval(x,y,30,30);
      Thread.sleep(40);
      g.setColor(Color.white);
      g.fillRect(0,0,d.width,d.height);
      y+=i;
      x++;
      c++;
    }
    catch(Exception e)
    {}

Here first the ground is displayed and then ball . After this the screen is refreshed by filling it with a white Rectangle of the size of the Applet window. The size of the Applet window is obtained using getSize() method.

In this falling loop the x – coordinate moves right with increment of 1 and y moves downwards with increment of 10.So, the ball moves diagonally forwards.

2.Distortion in the shape of ball

g.fillOval(x, y, 40, 28);

The size of the ball increases horizontally and decreases vertically as to imitate real effect when a ball hits the ground.

3.Upward movement of ball

while(y>380)
          { 
            // 3ball bouncing back from the ground upwards
            
            try{g.setColor(Color.black);g.fillRect(0, 430, 500, 450);
            g.setColor(Color.red);
            g.fillOval(x,y,30,30);
            Thread.sleep(30);
            g.setColor(Color.white);
            g.fillRect(0,0,d.width,d.height);
            y-=i;
            x++;
            c++;
          }catch(Exception e){}}

After the distortion phase the ball will move diagonally upwards as x coordinate increases by 1 and y coordinate decreases by 10.

4.Forward movement of ball

//in air
         for(int p=0;p<3;p++)
         {
           try{
             
             g.fillOval(x, y, 30, 30);
             Thread.sleep(5);
             g.setColor(Color.white);
             g.fillRect(0,0,d.width,d.height);
             g.setColor(Color.black);
             g.fillRect(0, 430, 500, 450);
             x++;
           }
           catch(Exception e)
           {}
         }

This step is used to give round shape to the bounces . Here only x coordinate is increased by 1 and it is repeated 3 times with a sleep of 5 nanoseconds each time.

 

Hence we get the final result as an animation of real like bouncing ball.

The following two tabs change content below.

Vijay Kashyap

Animation Programmer and Designer
Enthusiastic coder. Love to play games and make animations. Has a taste for designing and modelling. 🙂

Latest posts by Vijay Kashyap (see all)

You may also like...