home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / recenz / PROGRAM / JAVADRAW / iavadraw301_inst.exe / data.z / AnimatorApplication.java < prev    next >
Text File  |  1997-05-20  |  4KB  |  128 lines

  1. /*
  2.  * Copyright (c) 1995, 1996 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.  
  19. /* 
  20.  * Based on Arthur van Hoff's animation examples, this application
  21.  * can serve as a template for all animation applications.
  22.  */
  23. public class AnimatorApplication extends Frame implements Runnable {
  24.     int frameNumber = -1;
  25.     int delay;
  26.     Thread animatorThread;
  27.     boolean frozen = false;
  28.  
  29.     AnimatorApplication(int fps, String windowTitle) {
  30.         super(windowTitle);
  31.         delay = (fps > 0) ? (1000 / fps) : 100;
  32.     }
  33.  
  34.     public void startAnimation() {
  35.         //Create and start the animating thread.
  36.         if (frozen) {
  37.             //Do nothing.  The user has requested that we
  38.             //stop changing the image.
  39.         } else {
  40.             //Start animating!
  41.             if (animatorThread == null) {
  42.                 animatorThread = new Thread(this);
  43.             }
  44.             animatorThread.start();
  45.         }
  46.     }
  47.  
  48.     public void stopAnimation() {
  49.         //Stop the animating thread.
  50.         animatorThread = null;
  51.     }
  52.  
  53.     public boolean mouseDown(Event e, int x, int y) {
  54.         if (frozen) {
  55.             frozen = false;
  56.             startAnimation();
  57.         } else {
  58.             frozen = true;
  59.             stopAnimation();
  60.         }
  61.         return true;
  62.     }
  63.  
  64.  
  65.     public boolean handleEvent(Event e) {
  66.         switch (e.id) {
  67.           case Event.WINDOW_ICONIFY:
  68.             stopAnimation();
  69.             break;
  70.           case Event.WINDOW_DEICONIFY:
  71.             startAnimation();
  72.             break;
  73.           case Event.WINDOW_DESTROY:
  74.             System.exit(0);
  75.             break;
  76.         }  
  77.         return super.handleEvent(e);
  78.     }
  79.  
  80.     public void run() {
  81.         //Just to be nice, lower this thread's priority
  82.         //so it can't interfere with other processing going on.
  83.         Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  84.  
  85.         //Remember the starting time
  86.         long startTime = System.currentTimeMillis();
  87.  
  88.         //This is the animation loop.
  89.         while (Thread.currentThread() == animatorThread) {
  90.             //Advance the animation frame.
  91.             frameNumber++;
  92.  
  93.             //Display it.
  94.             repaint();
  95.  
  96.             //Delay depending on how far we are behind.
  97.             try {
  98.                 startTime += delay;
  99.                 Thread.sleep(Math.max(0, 
  100.                                       startTime-System.currentTimeMillis()));
  101.             } catch (InterruptedException e) {
  102.                 break;
  103.             }
  104.         }
  105.     }
  106.  
  107.     //Draw the current frame of animation.
  108.     public void paint(Graphics g) {
  109.         g.drawString("Frame " + frameNumber, 5, 50);
  110.     }
  111.  
  112.     public static void main(String args[]) {
  113.         AnimatorApplication animator = null;
  114.         int fps = 10;
  115.  
  116.         // Get frames per second from the command line argument
  117.         if (args.length > 0) {
  118.             try {
  119.                 fps = Integer.parseInt(args[0]);
  120.             } catch (Exception e) {}
  121.         }
  122.         animator = new AnimatorApplication(fps, "Animator");
  123.         animator.resize(200, 60);
  124.         animator.show();
  125.         animator.startAnimation();
  126.     }
  127. }
  128.