Programming / Computing

Bouncing Ball Project

 This program demonstrates a graphical Bouncing Ball Project....................................................    

 

The code for the same is as follows................................................................................  

 

 

import java.awt.*;

import java.awt.event.*;

class Screen extends Frame implements Runnable

{

int x,y,w,h;

Thread t;

int dir;

public Screen()

{

super("Bouncing Ball");

MyWindowAdapter adapter=new MyWindowAdapter(this);

addWindowListener(adapter);

x=400;y=100;w=50;h=50;

dir=1;

t=new Thread(this);

t.start();

}

public void run()

{

while(true)

{

Thread me=Thread.currentThread();

try

{

me.sleep(8);

}

catch(InterruptedException ie)

{}

repaint();

if(dir==1) y++;

else y--;

if(y>550)

{

dir=0;

}

if(y<30)

{

dir=1;

}

}

}

public void paint(Graphics g)

{

g.setColor(Color.red);

g.fillOval(x,y,w,h);

   setBackground(Color.yellow);

}

 

}

class Ball

{

public static void main(String arg[])

{

Screen s=new Screen();

s.setVisible(true);

s.setSize(800,600);

}

}

class MyWindowAdapter extends WindowAdapter

{

Screen s;

public MyWindowAdapter(Screen k)

{

 

s=k;

}

  public void windowClosing(WindowEvent we)

  {

s.setVisible(false);

System.exit(0);

}

}

 

© 2010 All rights reserved.

Make a free websiteWebnode