// // FILE: Ball.java // AUTHOR: Eugene Wallingford // DATE: 2012/09/18 // COMMENT: extracted from Ball class by Tim Budd in the textbook // Understanding Object-Oriented Programming with Java // // MODIFIED: 2012/10/08 -- Eugene Wallingford // CHANGE: Changed the deltaX and deltaY variables to be doubles, // rather than ints, to give us finer-grained control // over movement of the ball. // // MODIFIED: 2012/10/18 -- Eugene Wallingford // CHANGE: Added [x|y]DeltaIsPositive methods to give subclasses // access to direction of the ball's movement. // import java.awt.Color; import java.awt.Graphics; public class Ball extends Disk { private double deltaX; private double deltaY; public Ball( int x, int y, int r, double dx, double dy ) { super( x, y, r ); deltaX = dx; deltaY = dy; } public void move() { moveBy( (int) deltaX, (int) deltaY ); } protected void adjustSpeedBy( double dx, double dy ) { deltaX += dx; deltaY += dy; } protected void reverseDeltaX() { deltaX *= -1; } protected void reverseDeltaY() { deltaY *= -1; } // accessors for subclasses protected boolean xDeltaIsPositive() { return deltaX > 0; } protected boolean yDeltaIsPositive() { return deltaY > 0; } }