home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 4.3 KB | 228 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.Applet;
- import java.awt.image.ImageObserver;
-
- /*
- * a class describing a single ball
- */
- class Ball {
-
- /*
- * the image for this ball
- */
- Image img;
-
- /*
- * x position and velocity
- */
- double x, dx;
-
- /*
- * y position and velocity
- */
- double y, dy;
-
- /*
- * initialize the position and velocity
- * to random values
- */
- void random () {
-
- x = 10 + 380*Math.random ();
- y = 10 + 200*Math.random ();
- dx = 5 - 10*Math.random ();
- dy = 5 - 10*Math.random ();
- }
-
- /**
- * calculate the next position of this ball
- * and make sure it bounces off the edge of the panel
- * @param d - dimension of the bounding panel
- */
- void compute (Dimension d) {
-
- if (x <= 0 || x > d.width) dx = -dx; // bounce horizontal
- if (y <= 0 || y > d.height) dy = -dy; // bounce vertical
- x += dx;
- y += dy;
- }
-
- /**
- * draw the ball image
- * @param g - destination graphics object
- * @param obs - parent image observer
- */
- public void paint (Graphics g, ImageObserver obs) {
- g.drawImage (img, (int) x-10, (int) y-10, obs);
- }
- }
-
- /*
- * the panel containing the bouncing balls
- */
- class BouncePanel extends Panel implements Runnable {
-
- /*
- * the number of balls
- */
- final int nballs = 4;
-
- /*
- * the array holding all the balls
- */
- Ball balls[] = new Ball[10];
-
- /*
- * offscreen image
- */
- Image offimg;
-
- /*
- * size of offscreen image
- */
- Dimension offsize;
-
- /*
- * graphics object associated with offscreen image
- */
- Graphics offg;
-
- /*
- * thread for periodic updating
- */
- Thread thread;
-
- /*
- * the thread recalculates each ball position and
- * redraws them
- */
- public void run() {
-
- offsize = getSize();
- offimg = createImage (offsize.width, offsize.height);
- offg = offimg.getGraphics();
- while (true) {
- for (int i=0; i<nballs; i+=1) {
- balls[i].compute (offsize);
- }
- repaint ();
- try {
- Thread.sleep (25);
- } catch (InterruptedException e) {
- break;
- }
- }
- }
-
- /**
- * override update to avoid erase flicker
- * @param g - destination graphics object
- */
- public synchronized void update (Graphics g) {
-
- offg.setColor (Color.lightGray);
- offg.fillRect (0, 0, offsize.width, offsize.height);
-
- for (int i = 0 ; i < nballs ; i++)
- balls[i].paint (offg, this);
- offg.setColor (Color.black);
- offg.drawRect (0, 0, offsize.width-1, offsize.height-1);
- g.drawImage(offimg, 0, 0, this);
- }
-
- /*
- * start the update thread
- */
- public void start() {
-
- thread = new Thread(this);
- thread.start();
- }
-
- /*
- * stop the update thread
- */
- public void stop() {
-
- if (thread != null)
- {
- thread = null;
- }
- }
- }
-
- /*
- * the applet proper
- */
- public class Bounce extends Applet implements ActionListener {
-
- /*
- * instance of BouncePanel
- */
- BouncePanel panel;
- Button button1;
- /*
- * an array containing the images for the balls
- */
- Image img[] = new Image[4];
-
- /*
- * called when the applet is loaded
- * create an instance of bounce panel and add the Start button
- * and load images
- */
- public void init() {
-
- setLayout(new BorderLayout());
-
- panel = new BouncePanel ();
- add ("Center", panel);
- Panel p = new Panel ();
- add ("South", p);
-
- button1 = new Button("Start");
- p.add (button1);
- button1.addActionListener(this);
-
- img[0] = getImage (getDocumentBase(), "whiteball.gif");
- img[1] = getImage (getDocumentBase(), "redball.gif");
- img[2] = getImage (getDocumentBase(), "blueball.gif");
- img[3] = getImage (getDocumentBase(), "greenball.gif");
- for (int i=0; i<panel.nballs; i+=1) {
- panel.balls[i] = new Ball ();
- panel.balls[i].img = img[i & 3];
- }
- }
-
- /*
- * called when the applet is started
- * just start the bounce panel update thread
- */
- public void start() {
-
- panel.start();
- }
-
- /*
- * called when the applet is stopped
- */
- public void stop() {
-
- panel.stop();
- }
-
- /*
- * handle Start button press by randomizing balls
- * @param evt - event object
- * @param arg - target object
- */
-
- public void actionPerformed(ActionEvent evt)
- {
- for (int i=0; i<panel.nballs; i+=1)
- panel.balls[i].random ();
- }
-
- }
-