您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)

 

Content Index

Content Page # 20

The "BallWorld" class - version 4

If the ball keeps on moving its going to hit the edge of the window. Unless we add some code to make it stop or bounce the filled circle will just disappear out of sight.

We know the size of the window, since we created it in the "BallWorld" constructor method that creates a BallWorld object:

private BallWorld ()

{

// set size and title of our application window

setSize(600, 400);

setTitle("Ball World");

So the size of the window is 600 x 400 pixels. To make our code a little more general, rather than refer to, say, 600 each time we wish to refer to the width of the window, we will now introduce two new variables which store the values 600 and 400. Let's call them "frameWidth" and "frameHeight". Now, if we wished to change the width of the window we can just change the value in these variables once, and everywhere else in the class where we refer to these variables remains unchanged -- as opposed to have to find every occurrence of 600 and change to 800 if we wanted to make the window wider.

So we need to declare our two new variables:

public class BallWorld extends Frame

{

////////// variables ///////////

// application variables

// NEW NEW NEW - variables for width and height of frame

public static final int frameWidth = 600;

public static final int frameHeight = 400;

The above demonstrates how it is possible to both declare a variable, and also initialise it at the same time. We now have variable "frameWidth" set to 600 and "frameHeight" set to 400. (Don’t worry about the "public static final" terms for now -- these will be investigated in a later unit). We can now refer to these variables when we set the size of our window:

private BallWorld ()

{

// set size and title of our application window

// NEW NEW NEW - now set using variables "frameWidth", "frameHeight"

setSize(frameWidth, frameHeight);

setTitle("Ball World");

In order to make our ball bounce we need to test its X and Y location, and if it hits a side of the window (top, bottom, left or right) we need to change the direction of the corresponding "dx" or "dy" motion.

We can design our bounce tests and actions in pseudocode as follows:

if ( x value too small ) set the dx motion to the opposite direction

if ( x value too large ) set the dx motion to the opposite direction

if ( y value too small ) set the dy motion to the opposite direction

if ( y value too large ) set the dy motion to the opposite direction

We can refine our pseucode code to refer to our "frameWidth" and "frameHeight" variables, for when the X and Y values are too large. We can also refine our pseudocode design to use the "getX" and "getY" methods of myBall to refer to the current X and Y values of myBall:

if ( myBall.getX() < 0) set the dx motion to the opposite direction

if ( myBall.getX() > frameWidth ) set the dx motion to the opposite direction

if ( myBall.getY() < 0 ) set the dy motion to the opposite direction

if ( myBall.getY() > frameHeight ) set the dy motion to the opposite direction

We can further refine our pseucode to refer the dx and dy motion of the ball from the "getXMotion" and "getYMotion" methods. For example:

if ( myBall.getX() < 0) set new motion: -1 * myBall.getXMotion(), myBall.getYMotion 

since multipling our current X motion by -1 will reverse it.

So in our "paint" method, after sending the "move" message, we can write Java statements to implement our bounce actions:

public void paint(Graphics g)

{

myBall.paint(g);

myBall.move();

pause( 20 );

// NEW NEW NEW - change X or Y motion if ball touches side of window

// check for left of frame collision (X value too small)

if ( myBall.getX() < 0 )

myBall.setMotion( -myBall.getXMotion(), myBall.getYMotion() );

// check for right of frame collision (X value too large)

if ( myBall.getX() > FrameWidth )

myBall.setMotion(-myBall.getXMotion(), myBall.getYMotion());

// check for top of frame collision (Y value too small)

if ( myBall.getY() < 0 ) 

myBall.setMotion(myBall.getXMotion(), -myBall.getYMotion());

// check for bottom of frame collision (Y value too large)

if ( myBall.getY() > FrameHeight )

myBall.setMotion(myBall.getXMotion(), -myBall.getYMotion());

// NEW NEW NEW - bounce a lot more times !

if (counter < 500) 

{

counter = counter + 1;

repaint();

}

else 

System.exit(0);

}

As you may also notice, the pause value has been shortened to 20, and we are going to let myBall bounce 500 times all around the window. The full source file for "BallWorld.java - version 4" is available for viewing (and can be opened in the Kawa editor).

Perform activity 5 – Compilation and running of "BallWorld.java" - version 4

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg