home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / MTImageSequence.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  5.2 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 displays several images in a row.  It prevents
  22.  * flashing by double buffering.  MediaTracker preloads the image
  23.  * data using multiple background threads.  The program doesn't
  24.  * draw until all the images are fully loaded.
  25.  */
  26.  
  27. public class MTImageSequence extends Applet implements Runnable {
  28.     int frameNumber = -1;
  29.     int delay;
  30.     Thread animatorThread;
  31.     boolean frozen = false;
  32.  
  33.     Dimension offDimension;
  34.     Image offImage;
  35.     Graphics offGraphics;
  36.  
  37.     Image[] images;
  38.     MediaTracker tracker;
  39.  
  40.     public void init() {
  41.         String str;
  42.         int fps = 10;
  43.  
  44.         //How many milliseconds between frames?
  45.         str = getParameter("fps");
  46.         try {
  47.             if (str != null) {
  48.                 fps = Integer.parseInt(str);
  49.             }
  50.         } catch (Exception e) {}
  51.         delay = (fps > 0) ? (1000 / fps) : 100;
  52.  
  53.         //Load all the images.
  54.         images = new Image[10];
  55.         tracker = new MediaTracker(this);
  56.         for (int i = 1; i <= 10; i++) {
  57.             images[i-1] = getImage(getCodeBase(),
  58.                           "../../../images/duke/T"+i+".gif");
  59.             tracker.addImage(images[i-1], 0);
  60.         }
  61.     }
  62.  
  63.     public void start() {
  64.         if (frozen) {
  65.             //Do nothing.  The user has requested that we
  66.             //stop changing the image.
  67.         } else {
  68.             //Start animating!
  69.             if (animatorThread == null) {
  70.                 animatorThread = new Thread(this);
  71.             }
  72.             animatorThread.start();
  73.         }
  74.     }
  75.  
  76.     public void stop() {
  77.         //Stop the animating thread.
  78.         animatorThread = null;
  79.  
  80.         //Get rid of the objects necessary for double buffering.
  81.         offGraphics = null;
  82.         offImage = null;
  83.     }
  84.  
  85.     public boolean mouseDown(Event e, int x, int y) {
  86.         if (frozen) {
  87.             frozen = false;
  88.             start();
  89.         } else {
  90.             frozen = true;
  91.  
  92.             //Instead of calling stop(), which destroys the
  93.             //backbuffer, just stop the animating thread.
  94.             animatorThread = null;
  95.         }
  96.         return true;
  97.     }
  98.  
  99.     public void run() {
  100.         try {
  101.             //Start downloading the images. Wait until they're loaded
  102.         //before requesting repaints.
  103.             tracker.waitForAll();
  104.         } catch (InterruptedException e) {}
  105.  
  106.         //Just to be nice, lower this thread's priority
  107.         //so it can't interfere with other processing going on.
  108.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  109.  
  110.         //Remember the starting time.
  111.         long startTime = System.currentTimeMillis();
  112.  
  113.         //This is the animation loop.
  114.         while (Thread.currentThread() == animatorThread) {
  115.             //Advance the animation frame.
  116.             frameNumber++;
  117.  
  118.             //Display it.
  119.             repaint();
  120.  
  121.             //Delay depending on how far we are behind.
  122.             try {
  123.                 startTime += delay;
  124.                 Thread.sleep(Math.max(0, 
  125.                                       startTime-System.currentTimeMillis()));
  126.             } catch (InterruptedException e) {
  127.                 break;
  128.             }
  129.         }
  130.     }
  131.  
  132.     public void paint(Graphics g) {
  133.         update(g);
  134.     }
  135.  
  136.     public void update(Graphics g) {
  137.         Dimension d = size();
  138.  
  139.         //If not all the images are loaded, just clear the background
  140.         //and display a status string.
  141.         if (!tracker.checkAll()) {
  142.             g.clearRect(0, 0, d.width, d.height);
  143.             g.drawString("Please wait...", 0, d.height/2);
  144.         }
  145.  
  146.         //If all images are loaded, draw.
  147.         else {
  148.  
  149.             //Create the offscreen graphics context, if no good one exists.
  150.             if ( (offGraphics == null)
  151.               || (d.width != offDimension.width)
  152.               || (d.height != offDimension.height) ) {
  153.                 offDimension = d;
  154.                 offImage = createImage(d.width, d.height);
  155.                 offGraphics = offImage.getGraphics();
  156.             }
  157.  
  158.             //Erase the previous image.
  159.             offGraphics.setColor(getBackground());
  160.             offGraphics.fillRect(0, 0, d.width, d.height);
  161.             offGraphics.setColor(Color.black);
  162.  
  163.             //Paint the frame into the image.
  164.             offGraphics.drawImage(images[frameNumber % 10], 0, 0, this);
  165.  
  166.             //Paint the image onto the screen.
  167.             g.drawImage(offImage, 0, 0, this);
  168.         }
  169.     }
  170. }
  171.