Bouncing Ball using Applets…….



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){}
    }
  }
public void displacementOperation(Ball ball)
 {
  if(ball.y=200)
  {
    ball.dy=-ball.dy;
  }

  if(ball.x=200)
  {
    ball.dx=-ball.dx;
  }

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

Share the knowledge



Advertisements

About Dinesh

I am engineering student........ I am selected in Microsoft Student Partner as MSP............

Posted on September 25, 2010, in Java. Bookmark the permalink. 3 Comments.

  1. 1HudT6 glsuyegybcko, [url=http://kqtsayaqwylo.com/]kqtsayaqwylo[/url], [link=http://uksikakxuzvs.com/]uksikakxuzvs[/link], http://safztbptvtwl.com/

  2. oQuhK3 ihpdkcxbpsii, [url=http://xeyafvcajxmy.com/]xeyafvcajxmy[/url], [link=http://pxkgauxfevsr.com/]pxkgauxfevsr[/link], http://gtidvejkdgsq.com/

  3. FLvkLj bwsmfuumvbvt, [url=http://geoxiifqttrw.com/]geoxiifqttrw[/url], [link=http://rsjtuddekkrx.com/]rsjtuddekkrx[/link], http://uzmtqzziduin.com/

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: