The "BallWorld" class - version 2 We shall make a small change between versions 1 and 2 of our BallWorld class. Version 2 will display the ball twice -- in different positions on the screen (and with a long enough delay to make the two positions clear). To do this we shall introduce a new variable, called "counter". This variable is to count how many times we have painted our myBall object. This variable needs to be delared of type "int" so we can stored whole numbers in it. Thus the beginning of our new version of BallWorld will now looks as follows (don't worry about the "public" and "private" for now): public class BallWorld extends Frame { //////////////////////////////// ////////// variables /////////// //////////////////////////////// private Ball myBall; // NEW NEW NEW // a counter - so we can paint the ball more than once private int counter; We need to ensure that this variable is initialised with zero -- since when the application starts we will not have painted myBall any times. Thus we need to change our BallWorld constructor method to initialise our counter to zero: ////////// method BallWorld /////////// private BallWorld () { // set size and title of our application window setSize(600, 400); setTitle("Ball World"); // make "myBall" a reference to a new instance of ball myBall = new Ball(100, 100, 50, 4, 5 ); // NEW NEW NEW - set out counter to zero counter = 0; } As you may have realised, when our application executes and shows the window for out BallWorld object, the "paint" method of BallWorld is automatically executed. So we can put some more instructions in out BallWorld "paint" method, to use our "counter" variable to move myBall and then ensure method "paint" is called again to display myBall in its new positon. The pseudocode (informal) representation of how we need to change our BallWorld "paint" method is as follows (the first 2 steps as before): The "paint" method needs to be extended, so it sets the new (x, y) centre of the ball, repaints the ball and delays so we can see the change. ////////// method paint /////////// public void paint(Graphics g) { myBall.paint( g ); pause( 2000 ); // change ball position and increment counter myBall.move() // test counter, so will repaint again with new ball position if( counter < 1 ) { repaint(); counter = counter + 1; } else System.exit(0); } Each time out BallWorld receives a "repaint()" message it will cause the "paint()" method to be executed (we will not go into the reasons for sending a "repaint" message rather than a "paint" message -- this is dealt with in a later unit). The first time method "paint" is executed: a representation of the ball is displayed at the position determined by the initial settings (of 100, 100) myBall.paint( g ); the application pauses pause( 2000 ); we send the myBall object the message "move()" myBall.move() when myBall executes its "move()" method, it will add its movement values (dx was 4, dy was 5) to the current (x,y) centre of the object. When created, myBall had its centre initialised as (100, 100), so after the execution of the "move()" method the centre of myBalls circle will become (104, 105). a test is made to see if "paint" has been executed before if( counter < 1 ) this test succeeds (since "counter" is zero) so we send a "repaint" message repaint(); and also add 1 to our "counter" counter = counter + 1; so "counter" now replaces its previous value of 0 with the value 1
Upon receiving the "repaint" message, out BallWorld object will execute its "paint" method again after clearing any previous graphics in the window (so the filled circle is no longer visible). The second time method "paint" is executed: a representation of the ball is displayed at its new position (of 104, 105) myBall.paint( g ); the application pauses pause( 2000 ); we send the myBall object the message "move()" myBall.move() when myBall executes its "move()" method, it will add its movement values (dx was 4, dy was 5) to the current (x,y) centre of the object. Its centre is (104, 105), so after the execution of the "move()" method the centre of myBalls circle will become (108, 110). a test is made to see if "paint" has been executed before if( counter < 1 ) this test fails (since "counter" is 1) so we terminate the application and close the window System.exit(0);
At any point in time the screen looks much as it did version 1 of the application -- but as you watch it you should see the fill circle change position. The full source file for "BallWorld.java - version 2" is available for viewing (and can be opened in the Kawa editor). Back to top 
RITSEC - Global Campus Copyright ?1999 RITSEC- Middlesex University. All rights reserved. webmaster@globalcampus.com.eg |