home *** CD-ROM | disk | FTP | other *** search
/ Best Tools for JAVA / Best Tools for JAVA.iso / JAVA_ALL / IDE / SUBARTIC / SUB_ARCT / ANIM / ANIM_GEN.JAV < prev    next >
Encoding:
Text File  |  1996-10-04  |  4.6 KB  |  149 lines

  1. package sub_arctic.anim;
  2. import sub_arctic.lib.manager;
  3.  
  4. import java.awt.Component;
  5. import java.awt.Event;
  6.  
  7. /**
  8.  * This is the object which generates the animation events. It works by
  9.  * delivering a "fake" animation event (also known as a "tick"), then 
  10.  * sleeping for a short period of time.  Normally exactly one object of 
  11.  * this class is created by the static initializer at the end of this class
  12.  * and forked as a thread.<p>
  13.  * 
  14.  * Note: this class currently doesn't have any throttling behavior, and that 
  15.  * needs to be fixed. <P>
  16.  * 
  17.  * @author Ian Smith
  18.  */
  19. public class anim_generator implements Runnable {
  20.  
  21.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  22.  
  23.   /**
  24.    * This is how long we sleep between animation event generations
  25.    * (in milliseconds).  This defaults to 60.
  26.    */
  27.   protected static int _sleep_time=60;
  28.  
  29.   /**
  30.    * The thread which is providing the animation ticks
  31.    */
  32.   protected static Thread _anim_thread = null;
  33.  
  34.   /** 
  35.    * Retrieve the amount of time between animation events.
  36.    * @return int number of milliseconds between event generations
  37.    */
  38.   public static int sleep_time() { return _sleep_time;};
  39.  
  40.   /**
  41.    * Set the current amount of sleep time between animation events.
  42.    * The rate of animation events sets a lower bound on the accuracy
  43.    * of the animations (i.e. the size of the time intervals provided
  44.    * to animations.)
  45.    *
  46.    * @int x the new amount of sleep time between animation events (in 
  47.    *        milliseconds)
  48.    */
  49.   public static void set_sleep_time(int x) { _sleep_time=x;};
  50.  
  51.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  52.  
  53.   /**
  54.    * Indication of whether we need the pulses at all. The agent sets this value.
  55.    */
  56.   protected static boolean _need_ticks=false;
  57.  
  58.   /**
  59.    * Return whether we need the animation events (ticks) or not.
  60.    * If not, we don't bother delivering animation inputs to the 
  61.    * event dispatch system. <P>
  62.    * 
  63.    * The animation agent will turn on ticks when it has objects
  64.    * in its queues, and turn off ticks when it has no objects
  65.    * to consider.
  66.    *
  67.    * @return boolean true if we currently need ticks.
  68.    */
  69.   public static boolean need_ticks() { return _need_ticks;}
  70.  
  71.   /**
  72.    * Set the state of tick (animation event) generation.
  73.    * @param boolean b true if we need to be generating ticks to the event 
  74.    *                  dispatch code.
  75.    */
  76.   public synchronized static void set_need_ticks(boolean b) {
  77.     _need_ticks=b;
  78.     if (_anim_thread == null) {
  79.       if (_need_ticks) {
  80.     /* create a thread running on this class */
  81.     _anim_thread = new Thread(new anim_generator());
  82.     _anim_thread.start();
  83.       }
  84.     } else {
  85.       //xx SEH doesn't look like we ever call suspend(), so I'm taking this
  86.       //       out since it causes NS3.0 fits
  87.       //if (_need_ticks)
  88.       //  _anim_thread.resume();
  89.       /* setting _need_ticks to false will cause the thread to suspend itself */
  90.     }
  91.   }
  92.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  93.  
  94.   /**
  95.    * This is the method that gets run on the thread.  It delivers an 
  96.    * ANIMATION_EVENT event to some AWT component with a subArctic interface
  97.    * installed under it (which one doesn't matter since the animation agent
  98.    * will deliver it to the focus regardless), then sleeps from a short
  99.    * period of time.  
  100.    */
  101.   public void run() {
  102.     Component c;
  103.  
  104.     /* do this forever  */
  105.     while (true) {
  106.  
  107.       /* if we don't need ticks, don't bother */
  108.       if (_need_ticks) {
  109.  
  110.     /* get an awt component from the manager */
  111.     c=manager.an_awt_component();
  112.  
  113.     if (c!=null) {
  114.       /* send it the message ...with the current time in the event */
  115.       c.postEvent(new Event(c,animation_agent.ANIMATION_EVENT,
  116.                 new Long(System.currentTimeMillis())));
  117.     }
  118.       }
  119.  
  120.       try { 
  121.     Thread.currentThread().sleep(_sleep_time);
  122.       } catch (InterruptedException e) {
  123.     /* go ahead and deliver events... this will only happen on
  124.        EARLY exit, so it won't hurt */
  125.       }
  126.  
  127.     }
  128.   }
  129.  
  130.   /* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
  131. }
  132.  
  133. /*=========================== COPYRIGHT NOTICE ===========================
  134.  
  135. This file is part of the subArctic user interface toolkit.
  136.  
  137. Copyright (c) 1996 Scott Hudson and Ian Smith
  138. All rights reserved.
  139.  
  140. The subArctic system is freely available for most uses under the terms
  141. and conditions described in 
  142.   http://www.cc.gatech.edu/gvu/ui/sub_arctic/sub_arctic/doc/usage.html 
  143. and appearing in full in the lib/interactor.java source file.
  144.  
  145. The current release and additional information about this software can be 
  146. found starting at: http://www.cc.gatech.edu/gvu/ui/sub_arctic/
  147.  
  148. ========================================================================*/
  149.