import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class BouncingBallApplet extends Applet implements Runnable { // How many milliseconds between frames: int delay; double [] ballx=new double[1000]; double [] bally=new double[1000]; Color [] colorArray; // The current frame: int frameNumber; // A thread object. Thread animatorThread; public void init () { // We'll pause 500 ms between frames. delay = 50; colorArray=new Color[1000]; for (int i=0; i<1000; i++) { ballx[i]=100; bally[i]=100; colorArray[i]=new Color((float) Math.random(), (float) Math.random(), (float) Math.random()); } // We are going to use "null" as a true/false indicator. // Need to initialize to "false" (null). animatorThread = null; } // Start is called by the browser each time the applet // comes into view. public void start () { // Initially, we're at frame 0. frameNumber = 0; // If there isnt' an animator thread, create one. if (animatorThread == null) { animatorThread = new Thread (this); } // Start the independent thread. animatorThread.start (); } public void stop () { // Stop the animation by indicating "false". animatorThread = null; } public void run () { // Potentially, an infinite loop! while (animatorThread != null) { frameNumber ++; // Draw next frame. repaint (); // Delay try { Thread.sleep (delay); } catch (InterruptedException e) { break; } } } public void paint (Graphics g) { // Set foreground and background colors. setBackground (Color.white); g.setColor (Color.blue); // Here's how to get the size of the applet bounds within the program: // d.width is the width, d.height is the height. Dimension d = getSize(); for (int i=0; i<1000; i++) { g.setColor(colorArray[i]); g.fillOval((int) ballx[i],(int) bally[i],30,30); ballx[i]= ballx[i]+(10*Math.random()-5); bally[i]= bally[i]+(10*Math.random()-5); } } }