001    /*
002     * $Id: AnimateObject.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
003     *
004     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
005     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
006     * CS101 homepage</a> or email <las@ai.mit.edu>.
007     *
008     * Copyright (C) 1999 Massachusetts Institute of Technology.
009     * Please do not redistribute without obtaining permission.
010     */
011    package cs101.util;
012    
013    import cs101.lang.*;
014    
015    /**
016     * The generic self-animating (active) object.  Extend it, overriding
017     * its act() method, to generate interesting behavior.
018     *
019     * Copyright 1999 Massachusetts Institute of Technology
020     *
021     * @see cs101.lang.AnimatorThread
022     * @see cs101.lang.Animate
023     *
024     * @author Lynn Andrea Stein, las@ai.mit.edu
025     * @version $Id: AnimateObject.java,v 1.1.1.1 2002/06/05 21:56:32 root Exp $
026     *
027     */
028    
029    public abstract class AnimateObject extends Object implements Animate 
030    {
031      /* CONSTANTS */
032      
033      /**
034       * These constants allow mnemonic access to AnimateObject's final
035       * constructor argument, i.e., should the object start running on
036       * construction or on (a subsequent) call to a separate start()
037       * method?
038       *
039       * @see #AnimateObject( boolean ) 
040       */
041      public static boolean START_IMMEDIATELY = true,
042                            DONT_START_YET = false;
043    
044      /* FIELDS */
045    
046      /**
047       * The animacy that makes this object go.
048       *
049       * @see cs101.lang.AnimatorThread
050       */
051      protected AnimatorThread spirit;
052      
053    
054      /* CONSTRUCTORS */
055    
056      /**
057       * The most basic constructor creates the AnimateObject and starts
058       * its animacy going. 
059       */
060      public AnimateObject() 
061      {
062        this( AnimateObject.START_IMMEDIATELY );
063      }
064      
065      /**
066       * This constructor allows you to create an AnimateObject without
067       * starting its animacy going. (You do this by calling this
068       * constructor with <tt>AnimateObject.DONT_START_YET</tt>, i.e., the
069       * boolean <tt>false</tt>. If you do this, the animacy will not
070       * actually begin execution until you call this AnimateObject's
071       * start() method.
072       *
073       * @param startImmediately   If false, execution does not start until
074       *          explicit invocation of this object's start() method.
075       *
076       * @see #START_IMMEDIATELY
077       * @see #DONT_START_YET
078       */
079      public AnimateObject( boolean startImmediately ) 
080      {
081        super();
082        
083        this.spirit = new AnimatorThread(this);
084        if ( startImmediately )
085          {
086            this.spirit.startExecution();
087          }
088      }
089      
090      /**
091       * Invocation of this method begins this object's autonomous execution.  
092       *
093       * (If execution has already begun, this method does nothing.)
094       *
095       * @see #AnimateObject( boolean )
096       */
097      public void start() {
098        this.spirit.start();
099      }
100    
101      /**
102       * Override this method to give the AnimateObject behavior.
103       *
104       * The default behavior -- Thread.yield() -- simply gives other 
105       * animacies a chance to run.
106       */
107      public abstract void act();
108      
109    }
110    
111    /* Comments:
112     *
113     * History:
114     * $Log: AnimateObject.java,v $
115     * Revision 1.1.1.1  2002/06/05 21:56:32  root
116     * CS101 comes to Olin finally.
117     *
118     * Revision 1.4  1999/06/18 21:37:47  las
119     * Updated AnimateObject to work with the new AnimatorThread/Animate
120     * paradigm.  AnimateObject is now an abstract class; its act() method
121     * must be overridden to create a useful object.
122     *
123     * Revision 1.3 1998/07/24 17:19:21 tparnell
124     * Placate new javadoc behavior
125     *
126     * Revision 1.2 1998/06/24 20:58:36 tparnell
127     * formatting fixed
128     *
129     * Revision 1.1 1998/06/08 18:11:52 tparnell
130     * added files from Lynn
131     *
132     */