/* * ============================================================= * BouncePanel.java : Bounce circles around on the screen. * * Written by : Vasilios Lagakos March, 2001 * ============================================================== */ import javax.swing.*; import java.awt.*; public class BouncePanel extends JComponent implements Runnable { private MovingCircle circle; private int width, height; private Thread animationThread = null; public BouncePanel() { } //---------------------------------------------------- /*** Skip the usual screen-clearing step of update * so that there is no "flicker" between each * drawing step. */ public void update(Graphics g){ paint(g); } //---------------------------------------------------- /*** Draws the circle in its new location */ public void paint(Graphics g) { width = getSize().width; height = getSize().height; /* If you do not check if "circle" is not null before using it to draw a circle * you will get a NullPointerException, a Runtime exception that occurs when a * method tries to access a member of an object through a null reference. This * NullPointerException occurs because when the JApplet starts it automatically * calls this paint method and executes everything in the paint method. However, * when the JApplet starts we have not pressed the start button yet and thus * have not created the MovingCircle object, "circle". Therefore "circle" tries * to access a method through a null reference. */ if(circle != null) { circle.move(width, height); circle.draw(g, width, height); } } //---------------------------------------------------- /*** When the "start" button is pressed, start the * animation thread if it is not already started. * Create a new MovingCircle. */ public void start() { int radius = 25; int x = radius + randomInt(width - 2 * radius); int y = radius + randomInt(height - 2 * radius); int deltaX = 1 + randomInt(10); int deltaY = 1 + randomInt(10); circle = new MovingCircle(x, y, radius, deltaX, deltaY); animationThread = new Thread(this); animationThread.start(); } //---------------------------------------------------- /*** When the "stop" button is pressed, stop * the thread. */ public void stop() { if (animationThread != null) { animationThread = null; } } //---------------------------------------------------- /** Each time around the loop, call paint and then * take a short pause. The paint method will * move the circles and draw them. */ public void run() { Thread myThread = Thread.currentThread(); // Really while animationThread not null while(animationThread==myThread) { repaint(); pause(100); } } //---------------------------------------------------- // Returns an int from 0 to max (inclusive), // yielding max + 1 possible values. private int randomInt(int max) { double x = Math.floor((double)(max + 1) * Math.random()); return((int)(Math.round(x))); } //---------------------------------------------------- // Sleep for the specified amount of time. private void pause(int milliseconds) { try { Thread.sleep((long)milliseconds); } catch(InterruptedException ie) {} } } // end of class BouncePanel