home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / MovingImage.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.0 KB  |  171 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.applet.Applet;
  19.  
  20. /* 
  21.  * This applet moves one image in front of a background image.
  22.  * It eliminates flashing by double buffering.
  23.  */
  24.  
  25. public class MovingImage extends Applet implements Runnable {
  26.     int frameNumber = -1;
  27.     int delay;
  28.     Thread animatorThread;
  29.     boolean frozen = false;
  30.  
  31.     Dimension offDimension;
  32.     Image offImage;
  33.     Graphics offGraphics;
  34.  
  35.     Image stars;
  36.     Image rocket;
  37.  
  38.     public void init() {
  39.         String str;
  40.         int fps = 10;
  41.  
  42.         //How many milliseconds between frames?
  43.         str = getParameter("fps");
  44.         try {
  45.             if (str != null) {
  46.                 fps = Integer.parseInt(str);
  47.             }
  48.         } catch (Exception e) {}
  49.         delay = (fps > 0) ? (1000 / fps) : 100;
  50.  
  51.         //Get the images.
  52.         stars = getImage(getCodeBase(), "../images/starfield.gif");
  53.         rocket = getImage(getCodeBase(), "../images/rocketship.gif");
  54.     }
  55.  
  56.     public void start() {
  57.         if (frozen) {
  58.             //Do nothing.  The user has requested that we
  59.             //stop changing the image.
  60.         } else {
  61.             //Start animating!
  62.             if (animatorThread == null) {
  63.                 animatorThread = new Thread(this);
  64.             }
  65.             animatorThread.start();
  66.         }
  67.     }
  68.  
  69.     public void stop() {
  70.         //Stop the animating thread.
  71.         animatorThread = null;
  72.  
  73.         //Get rid of the objects necessary for double buffering.
  74.         offGraphics = null;
  75.         offImage = null;
  76.     }
  77.  
  78.     public boolean mouseDown(Event e, int x, int y) {
  79.         if (frozen) {
  80.             frozen = false;
  81.             start();
  82.         } else {
  83.             frozen = true;
  84.  
  85.             //Instead of calling stop(), which destroys the
  86.             //backbuffer, just stop the animating thread.
  87.             animatorThread = null;
  88.         }
  89.         return true;
  90.     }
  91.  
  92.     public void run() {
  93.         //Just to be nice, lower this thread's priority
  94.         //so it can't interfere with other processing going on.
  95.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  96.  
  97.         //Remember the starting time.
  98.         long startTime = System.currentTimeMillis();
  99.  
  100.         //This is the animation loop.
  101.         while (Thread.currentThread() == animatorThread) {
  102.             //Advance the animation frame.
  103.             frameNumber++;
  104.  
  105.             //Display it.
  106.             repaint();
  107.  
  108.             //Delay depending on how far we are behind.
  109.             try {
  110.                 startTime += delay;
  111.                 Thread.sleep(Math.max(0, 
  112.                                       startTime-System.currentTimeMillis()));
  113.             } catch (InterruptedException e) {
  114.                 break;
  115.             }
  116.         }
  117.     }
  118.  
  119.     public void paint(Graphics g) {
  120.         update(g);
  121.     }
  122.  
  123.     public void update(Graphics g) {
  124.         Dimension d = size();
  125.  
  126.         //Create the offscreen graphics context, if no good one exists.
  127.         if ( (offGraphics == null)
  128.           || (d.width != offDimension.width)
  129.           || (d.height != offDimension.height) ) {
  130.             offDimension = d;
  131.             offImage = createImage(d.width, d.height);
  132.             offGraphics = offImage.getGraphics();
  133.         }
  134.  
  135.         //Erase the previous image.
  136.         offGraphics.setColor(getBackground());
  137.         offGraphics.fillRect(0, 0, d.width, d.height);
  138.         offGraphics.setColor(Color.black);
  139.  
  140.         //Paint the frame into the image.
  141.         paintFrame(offGraphics);
  142.  
  143.         //Paint the image onto the screen.
  144.         g.drawImage(offImage, 0, 0, this);
  145.     }
  146.  
  147.     void paintFrame(Graphics g) {
  148.         Dimension d = size();
  149.         int w;
  150.         int h;
  151.  
  152.         //If we have a valid width and height for the background image,
  153.         //draw it.
  154.         w = stars.getWidth(this);
  155.         h = stars.getHeight(this);
  156.         if ((w > 0) && (h > 0)) {
  157.             g.drawImage(stars, (d.width - w)/2,
  158.                         (d.height - h)/2, this);
  159.         }
  160.  
  161.         //If we have a valid width and height for the background image,
  162.         //draw it.
  163.         w = rocket.getWidth(this);
  164.         h = rocket.getHeight(this);
  165.         if ((w > 0) && (h > 0)) {
  166.             g.drawImage(rocket, ((frameNumber*5) % (w + d.width)) - w,
  167.                         (d.height - h)/2, this);
  168.         }
  169.     }
  170. }
  171.