import java.awt.*; public class Ball { int x, y, size, speed; int dirX, dirY; int appletWdt, appletHgt; public Ball( int _x, int _y, int _size, int _speed, int w, int h ) { x = _x; // (x, y) coordinates of ball y = _y; size = _size; // width and height are same size speed = _speed; // speed is number of pixels to move each time dirX = 1; // direction is either +1 go right or -1 go left dirY = 1; // direction is either +1 go down or -1 go up appletWdt = w; // applet width appletHgt = h; // applet height } public void paint( Graphics g ) // called from applet { g.setColor( Color.BLUE ); g.fillOval( x, y, size, size ); } public void move( ) { // move ball based on speed and direction x = x + speed * dirX; y = y + speed * dirY; if ( x < 0 ) // hit left boundary, change direction dirX = 1; else if ( x > appletWdt ) // hit right boundary, change direction dirX = -1; if ( y < 0 ) // hit top boundary, change direction dirY = 1; else if ( y > appletHgt ) // hit bottom boundary, change direction dirY = -1; } }