www.pudn.com > TWPlaneGame.rar > ActionUpdater.java


package twplanegame;

import java.util.*;

class ActionUpdater extends TimerTask
{
   // list of all the listeners
   private final ActionListener[]   m_listener = new ActionListener[20];
   private final Timer              m_updateTimer = new Timer();

   /**
    * Creates a ActionUpdater object and starts it.
    *
    * @param initSleep Initial sleep time.
    */
   public ActionUpdater(int initSleep, int interval)
   {
      m_updateTimer.schedule(this, initSleep, interval);
   }

   /**
    * Registers the ActionListener that should be informed of an action to be
    * performed.
    *
    * @param listener The listener that should be informed of an action to be
    * performed.
    * @return true if it was registered; false otherwise
    */
   public boolean register(ActionListener listener)
   {
      // find an empty spot, and save the reference
      for (int i = 0; i < m_listener.length; i++)
      {
         if (m_listener[i] == null)
         {
            m_listener[i] = listener;
            return true;
         }
      }
      return false;
   }

   /**
    * Perform the repetition of the key.
    */
   public void run()
   {
      // ask each listener to perform an action
      for (int i = 0; i < m_listener.length; i++)
      {
         if (m_listener[i] != null)
         {
            m_listener[i].performAction();
         }
      }
   }//void run()
}//class ActionUpdater