// Raja Sooriamurthi 11/13/99 import java.awt.*; public class MoveableBall extends Ball implements Moveable { static final int R=3; Component cmp; double theta; // direction of motion int dx=3, dy=6; MoveableBall (Component cmp) { super(cmp); this.cmp = cmp; theta = 2*Math.PI*Math.random(); dx = (int) (R*Math.cos(theta)); dy = (int) (R*Math.sin(theta)); } MoveableBall (Component cmp, Point p) { super(p); this.cmp = cmp; theta = 2*Math.PI*Math.random(); dx = (int) (R*Math.cos(theta)); dy = (int) (R*Math.sin(theta)); } public void erase (Graphics g) { g.setColor(cmp.getBackground()); g.fillOval (getX()-radius, getY()-radius, 2*radius, 2*radius); } public void move() { Dimension dim = cmp.getSize(); if ((getY()<0) || (getY()>dim.height)) theta = -theta; if ((getX()<0) || (getX()>dim.width)) theta = Math.PI/2-(theta-Math.PI/2); dx = (int) (R*Math.cos(theta)); dy = (int) (R*Math.sin(theta)); position.translate(dx, dy); } }