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

Posted

in

by


Related Posts

Comments

6 responses to “Bouncing ball animation in Applet”

  1. […] Bouncing ball applet – Create Balls on Mouse click of random color and random size Posted by admin on September 9th, 2010 In previous example, we have seen that how to animate two balls around the applet border. […]

  2. Muzaffar Shah Khan Avatar
    Muzaffar Shah Khan

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

    public void displacementOperation(Ball ball)

  3. admin Avatar

    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.

  4. asha Avatar
    asha

    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.

  5. asha Avatar
    asha

    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);
    }

  6. jobs Avatar
    jobs

    how to insert a background color o image?

Leave a Reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading