import java.awt.*; import java.awt.event.*; public class BouncingBallsApplet3 extends SimpleAnimationApplet implements MouseMotionListener { BouncingBall[] balls; static final int ballCount = 5; Color mycolor; public void init() { addMouseMotionListener(this); int w = getSize().width; int h = getSize().height; balls = new BouncingBall[ballCount]; for (int i = 0; i < ballCount; i++){ int r=(int)(256*Math.random()); int g=(int)(256*Math.random()); int b=(int)(256*Math.random()); mycolor=new Color(r,g,b); balls[i] = new BouncingBall(mycolor,w/2,h/2,10); } setMillisecondsPerFrame(50); } public void drawFrame(Graphics g, int width, int height) { g.setColor(Color.white); g.fillRect(0,0,width,height); g.setColor(Color.black); g.drawRect(0,0,width-1,height-1); for (int i = 0; i < ballCount; i++) balls[i].doFrame(g,width,height); } public void mouseDragged(MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); for (int i = 0; i < ballCount; i++) balls[i].headTowards(x,y); } public void mouseMoved(MouseEvent evt) {} }