import java.awt.*; import java.awt.geom.*; /** * A Ball lives in a Box and can be moved and changed in various ways. */ public class Ball{ private int centerX, centerY; private int radius = 20; private int stepX, stepY = 0; private Color color = Color.BLACK; private Box box; /** * Create a black Ball with radius 20 and velocity zero * and center at (x,y) in Box b. */ public Ball(Box b,int x, int y){ box = b; centerX = x; centerY = y; box.addBall(this); box.repaint(); } /** * Move this Ball a one time unit step, i.e. change center position * with the current velocity. */ public void step() { centerX = centerX + stepX; centerY = centerY + stepY; box.repaint(); } public void setStepSizes(int dx, int dy){ stepX = dx; stepY = dy; } public void setRadius(int r){ radius=r; box.repaint(); } public void setColor(Color col) { color = col; box.repaint(); } public void paint(Graphics g){ Graphics2D g2 = (Graphics2D)g; g2.setColor(color); g2.fill(new Ellipse2D.Double(centerX-radius,centerY-radius,2*radius,2*radius)); } }