home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 4.7 KB | 245 lines |
- import java.awt.*;
- import java.awt.event.*;
- import java.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; // x position and velocity
-
- /*
- * y position and velocity
- */
- double y, dy; // y position and velocity
-
- /*
- * 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;
-
- /*
- * an array containing the images for the balls
- */
- Image img[] = new Image[4];
-
- /*
- * the audio clip to be played in a loop
- */
- AudioClip sound;
- Button btnStart;
- Button btnStop;
- /*
- * 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);
- btnStart = new Button("Start");
- p.add (btnStart);
- btnStop = new Button("Stop");
- p.add (btnStop);
- btnStart.addActionListener(this);
- btnStop.addActionListener(this);
-
- sound = getAudioClip(getCodeBase(), "sound.au");
-
- 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
- * Don't do anything
- */
- public void start () {
-
- }
-
- /*
- * Called when the applet is stopped
- */
- public void stop() {
-
- panel.stop();
- sound.stop ();
- }
-
- /*
- * Handle start button press by randomizing balls
- */
- public void actionPerformed(ActionEvent ev)
- {
- Object object1 = ev.getSource();
- if (object1 == btnStart)
- {
- for (int i=0; i<panel.nballs; i+=1)
- panel.balls[i].random ();
- panel.start ();
- sound.loop ();
- }
- if (object1 == btnStop)
- {
- sound.stop ();
- }
- }
-
- }
-