AP Java Assignment 4
"Bouncing Ball" class

Background: Threads and the Applet class

All animation is based on the same Erase-Move-Draw-Wait sequence. In a C++ application we would just use a delay to make the computer wait, but you wouldn’t want to do that in an applet. Using a simple delay would make EVERY PROCESS the computer was working stop for the specified period of time. That’s unacceptable in when an applet is just one of several active items on a computer. Instead, we need a way that will make ONLY OUR APPLET wait for the specified time. To do this we will use a thread.

public class Apjava4 extends Applet implements Runnable
{
    Thread animation;  //declares a thread called animation
    static final int REFRESH_RATE = 50;  //Constant for “delay”
    
    //declare other class data members
    
    public void init()
    { 
       //initialize class data members
        
    }
    public void paint(Graphics g)
    {

		 //painting
    }
    public void start()
    {
	    animation = new Thread(this);
          if(animation != null)
          {
	      animation.start();
          }
    }
    public void run()
    {
         while(true)
         {
            //put code that runs in loop here
            repaint();
            try
            {
	         Thread.sleep(REFRESH_RATE);
            }catch(Exception exc){};
         }
    }

    public void stop() 
    {
	    animation = null;
    }

}

The "Bouncing Ball" class

  1. Write a "Bouncing Ball" class. The class definition should be outside your "Applet" class, you can even put it in a seperate file if you want. The class should have the following data members:
    1. The X and Y coordinates of the ball
    2. The "speed" of the ball (the distance the ball moves each turn).
    3. The acceleration due to gravity
    4. You may also want data members for the Color and size of the ball.
  2. Your ball class will also need several methods:
    1. BouncingBall() the class constructor. This will initialize the data members.
    2. void move () moves the ball by first checking to see if the ball has "hit" the bottom of the applet. If it has hit bottom, move changes the speed of the ball from positive to negative. Otherwise, the speed of the ball is increased by the amount of acceleration. Then the speed of the ball is added to the Y coordinate.
    3. void draw (Graphics g) draws the ball at its current position.
  3. Once you’ve finished writing the class, you’ll want to declare and initialize an instance of the ball class as a data member in the "Applet" class. Move the ball in the Applet's while loop and draw the ball in the Applet's paint method.
  4. You should now be able to display the bouncing ball to the screen.

Adding Double Buffering (optional)

Double buffering introduces a second "invisible" screen. All drawing is done offscreen first, and then copied all at once to the visible screen. This eliminates screen flicker. To implement double buffering you will need to make 6 changes:
public class Apjava4 . . . .
{
     //1.  two added declarations
	Graphics offscreen;
	Image image;
	
     public void init()
     {
		. . . .
        
             //2.  two added initializations
             image = createImage(nSCREEN_WIDTH, nSCREEN_HEIGHT);
             offscreen = image.getGraphics();
     }
     public void paint(Graphics g)
     {
		//3.  clear offscreen
		offscreen.setColor(Color.white);
		offscreen.fillRect(0,0,nSCREEN_WIDTH, nSCREEN_HEIGHT);
		
		//4.  modify code so that drawing is offscreen
		theBall.draw(offscreen);
                . . . . 
		
		//5.  copy offscreen to visible screen
		g.drawImage(image,0,0,this);
     }
	   
     //6.  override update()
     public void update(Graphics g)
     {
   		paint(g);
     }
	
}

Extensions

If you have extra time, you can add extra features to your applet: have the ball bounce off the other 3 sides of the applet, add a paddle that lets a player bounce the ball, change the size or color of the ball with each bounce, etc. Slime Volleyball is a good example of a gravity-based pong game. Below is a single player version. Click and drag the blue paddle to bounce the ball.

You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.

Submit the URL of your applet with the attached Java code (the .java file(s)) in an email message to mrsimon@lycos.com.