// same as BouncingBall but with an overloaded move method to move based on parameters received of a new dx and dy value, to be controlled by keyboard import java.awt.*; // for Color import java.util.*; // for Random public class BouncingBall2 // represents a Ball to be drawn on a Graphics object that will bounce around as it moves { private int x, y, dx, dy, borderx, bordery; // x,y coordinate of ball, dx,dy is the motion of the ball, borderx,bordery is used to rebound ball if it hits right or lower edge private Color c; // Color of ball, selected randomly private Random generator; // used to determine initial motion and rebound motion private int size; // the size of the ball (this could be assigned randomly, passed in via a constructor or hard-coded, we will hard-code in this example) public BouncingBall2(int bx, int by, Random g) // initialize borders and Random number generator given parameters { size=10; borderx=bx; // set borders bordery=by; generator=g; // set Random number generator x=generator.nextInt(bx-10)+10; // establish random x,y location at least 5 in from any edge y=generator.nextInt(by-10)+10; do { // get motion but do not let this ball have an initial motion of 0 in both directions dx=generator.nextInt(9)-4; // dx is how far to move x each time the Timer sends an ActionEvent dy=generator.nextInt(9)-4; // dy is how far to move y each time the Timer sends an ActionEvent }while(dx==0&&dy==0); // if both are 0, the ball will not move! int red=generator.nextInt(200); // get a random color, but only use rgb values between 0 and 199 so that the ball does not appear to close to white or it will int green=generator.nextInt(200); // not be visible int blue=generator.nextInt(200); c=new Color(red,green,blue); // set the Color given the random r,g,b values } public void move(int newdx, int newdy) { x+=newdx; y+=newdy; if(x>borderx-5) // if the movement puts the ball too close to the right border { // then move it back and change its horizontal motion to the opposite direction x-=dx; dx*=-1; } else if(x<5) // if the movement puts the ball too close to the left border { // then move it back and change its horizontal motion to the opposite direction x-=dx; dx*=-1; } if(y>bordery-5) // do the same for the bottom border, changing the vertical motion { y-=dy; dy*=-1; } else if(y<5) // and the same for the top border { y-=dy; dy*=-1; } } public void move() // moves the ball and tests to see if in moving it, it hits (comes close to) a border { x+=dx; // move the ball a small amount based on the values of dx and dy y+=dy; if(x>borderx-5) // if the movement puts the ball too close to the right border { // then move it back and change its horizontal motion to the opposite direction x-=dx; dx*=-1; } else if(x<5) // if the movement puts the ball too close to the left border { // then move it back and change its horizontal motion to the opposite direction x-=dx; dx*=-1; } if(y>bordery-5) // do the same for the bottom border, changing the vertical motion { y-=dy; dy*=-1; } else if(y<5) // and the same for the top border { y-=dy; dy*=-1; } } public void draw(Graphics g) // draw the BouncingBall onto the Graphics object { g.setColor(c); g.fillOval(x,y,size,size); } // the following methods are needed to support the change from the BouncingBalls program to the BouncingBallsWithRebound program public int getX() {return x;} // accessors needed for the collide method public int getY() {return y;} public boolean collide(BouncingBall b) // compare this BouncingBall and another BouncingBall b to see if they have collided { int bx=b.getX(); // get b's x,y coordinates int by=b.getY(); if(x>=bx&&x<=bx+size&&y>=by&&y<=by+size) // see if they touch (this BouncingBall's x,y coordinate lies somewhere within b return true; // and if so, return true else return false; // otherwise return false } public void rebound() // when told to rebound, this BouncingBall generates a new dx,dy value ensuring that both are not 0 { do { // we could have done a normal rebound by doing dx*=-1;dy*=-1; but this gives a more interesting result dx=generator.nextInt(9)-4; dy=generator.nextInt(9)-4; }while(dx==0&&dy==0); } }