Bouncing ball animation in Applet

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags , , with 6 Comments on Bouncing ball animation in Applet

Example to show Bouncing ball animation in Applet

In Previous article, we have seen the life cycle of Applet.

In this example, i will dem0nstrate that how we can animate ball and bounce it around the walls.

Please give your feedback on this article, so that i can improve it in future.

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

class Ball
{
  int x,y,radius,dx,dy;
  Color BallColor;

 public Ball(int x,int y,int radius,int dx,int dy,Color bColor)
 {
   this.x=x;
   this.y=y;
   this.radius=radius;
   this.dx=dx;
   this.dy=dy;
   BallColor=bColor;
 }
}
public class BouncingbALL extends Applet implements Runnable{
Ball redBall,blackBall;
  public void init()
  {
    redBall=new Ball(80,80,20,2,4,Color.red);
    blackBall=new Ball(40,70,20,4,2,Color.black);
    Thread t=new Thread(this);
    t.start();
  }

 public void paint(Graphics g)
 {
    g.setColor(redBall.BallColor);
    g.fillOval(redBall.x, redBall.y, redBall.radius, redBall.radius);

    g.setColor(blackBall.BallColor);
    g.fillOval(blackBall.x, blackBall.y, blackBall.radius, blackBall.radius);
 }

  public void run()
  {
    while(true)
    {
     try
        {
           displacementOperation(redBall);
           displacementOperation(blackBall);
           Thread.sleep(20);
           repaint();
        }
    catch(Exception e){}
    }
  }
//This method checks the boundary condition of ball movement
public void displacementOperation(Ball ball)
 {
  if(ball.y >= 200 || ball.y <= 0)
  {
    ball.dy=-ball.dy;
  }

  if(ball.x >= 200 || ball.x <= 0)
  {
    ball.dx=-ball.dx;
  }

   ball.y=ball.y-ball.dy;
   ball.x=ball.x-ball.dx;
  }
}

Mouse handling in applets using this pointer method, Bouncing ball

Related posts

6 thoughts on “Bouncing ball animation in Applet”

  1. Can u explain me the logic in method below please!! thanks in advance

    public void displacementOperation(Ball ball)

  2. Hi Muzaffar,
    displacementOperation() method checks the boundary condition for ball object. lets say if balls x coordinate is crossing the maximum allowed values it should be re bounce.

  3. Hi Shiva, works beautifully. But I was puzzled by it.
    Please try…
    public void paint(Graphics g)
    { System.out.println(“repaint called paint method of component”);
    g.setColor(redBall.BallColor);
    .other code that is already there…}
    public void run()
    {…..
    repaint();
    System.out.println(“thread is still running .”);
    ……
    .other code that is already there…}
    Run the applet you will see that the first message stops and starts as the applet is minimized and maximized as applets should do…
    But looks like that it is so because repaint() does not call paint(Graphics g) when the frame is minimized. The second message still prints out and CPU cycle is consumed. Maybe thread synchronization should be used in start and stop methods to actually pause the execution for it to work like a true applet?Anyways it works beautifully for the present purpose.

  4. Hi , After a lot of effort I managed to make it work like I expected. Some additions and some changes to the original code.
    added…
    boolean active;//instance variable@Override
    public void start() {
    super.start();
    System.out.println(“start”);
    synchronized (this) {
    active=true;
    notify();
    } }
    @Override
    public void stop() {
    super.stop();
    System.out.println(“stop”);
    synchronized (this) {
    active=false;
    notify();
    } }

    changed…
    @Override
    public void run() {
    do{
    do{
    displacementOperation(redBall);
    displacementOperation(blackBall);
    repaint();
    try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
    System.out.println(“requested repaint”);
    }while(active==true);
    synchronized (this) {
    System.out.println(“entering wait”);
    try { wait(); } catch (InterruptedException e) { e.printStackTrace(); }
    System.out.println(“out of wait”);
    }
    System.out.println(“thread is still running….”);
    }while(true);
    }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.