import java.awt.Color; /** * A Ball object represents the data model for a single bouncing ball. * A ball has a position, size, color, and velocity. * As it bounces off walls it simply reverses its x or y velocity. * @author Marty Stepp * @version CSE 331 Spring 2011, 5/18/2011 */ public class Ball { private int x; // position private int y; private int size; // size private int dx; private int dy; // velocity private Color color; /** * Constructs a new Ball with the given properties. */ public Ball(int x, int y, int size, int dx, int dy, Color color) { this.x = x; this.y = y; this.size = size; this.dx = dx; this.dy = dy; this.color = color; } /** * Signifies that this ball has bounced off the left or right wall, * which will cause it to reverse its x velocity. */ public void bounceLeftRight() { dx = -dx; } /** * Signifies that this ball has bounced off the top or bottom wall, * which will cause it to reverse its y velocity. */ public void bounceTopBottom() { dy = -dy; } /** * Moves this ball by the amounts specified as its x and y velocity. */ public void move() { x += dx; y += dy; } /** * Returns this ball's color. */ public Color getColor() { return color; } /** * Returns this ball's leftmost x-coordinate. */ public int getX() { return x; } /** * Returns this ball's rightmost x-coordinate. */ public int getRightX() { return x + size; } /** * Returns this ball's topmost y-coordinate. */ public int getY() { return y; } /** * Returns this ball's bottommost y-coordinate. */ public int getBottomY() { return y + size; } /** * Returns this ball's width/height size. */ public int getSize() { return size; } /** * Returns this ball's velocity in the x-axis. * Every time the ball moves, it will shift its x-coordinate by this many pixels. */ public int getDX() { return dx; } /** * Returns this ball's velocity in the y-axis. * Every time the ball moves, it will shift its y-coordinate by this many pixels. */ public int getDY() { return dy; } }