import javax.swing.*; import java.awt.*; /** * Ball.java * * A class representing a single circular ball for use * in simple 2D animations. See World.java. * * @author Jeff Ondich * * @version 1.0.0 * date: 02.09.2010 */ public class Ball { private int centerX; private int centerY; private int radius; private int velocityX; private int velocityY; private Color color; public Ball(int x, int y, int r, int vx, int vy, Color c) { centerX = x; centerY = y; radius = r; velocityX = vx; velocityY = vy; color = c; } // Draw the Ball object in the Graphics object g. public void draw(Graphics g) { g.setColor(color); g.fillOval(centerX - radius, centerY - radius, 2*radius, 2*radius); } // Move the Ball object one step in the direction and magnitude of // its velocity. public void step() { centerX += velocityX; centerY += velocityY; } // ======= Accessors ======= public int getX() { return centerX; } public int getY() { return centerY; } public void setCenter(int x, int y) { centerX = x; centerY = y; } public int getRadius() { return radius; } public void setRadius(int r) { if( r > 0 ) radius = r; } public int getVelocityX() { return velocityX; } public int getVelocityY() { return velocityY; } public void setVelocity(int vx, int vy) { velocityX = vx; velocityY = vy; } public Color getColor() { return color; } public void setColor(Color newColor) { color = newColor; } }