scg/ch06/CountdownTimer

From FANG

Jump to: navigation, search

001 package scg.ch06;
002 
003 import fang2.attributes.Palette;
004 import fang2.sprites.StringSprite;
005 
006 /**
007  * Countdown timer: A {@link fang2.sprites.StringSprite StringSprite}
008  * -derived class which counts down from a given time.
009  */
010 public class CountdownTimer
011   extends StringSprite {
012   /** the time remaining to be counted down in seconds */
013   private double remainingTime;
014 
015   /**
016    * Construct new timer. Timer set to 0.0 seconds and then turned off.
017    */
018   public CountdownTimer() {
019     remainingTime = 0.0;
020     hide();// make the timer invisible
021   }
022 
023   /**
024    * This method is called once from {@code advance} when remaining time
025    * crosses from positive to non-positive. This is where the "action"
026    * of the timer goes. For our purposes, the action is to turn off (set
027    * visibility off).
028    */
029   private void finishCountdown() {
030     hide();
031   }
032 
033   /**
034    * Update the text to display the ceiling of the seconds remaining
035    * (the integer just above the number) and update the opacity of the
036    * current color. Using the (int) cast, we convert remainingTime into
037    * the floor (the largest integer smaller or equal to the
038    * remainingTime). We use that to calculate the fractional part of the
039    * second remaining (e.g. remainingTime = 2.24 implies (int)
040    * remainingTime = 2 and remainingTime - (int) remainingTime = 0.24).
041    * We need the fraction converted to a value between 0 and 255: if the
042    * fractional part is 0.5, we want 128 and if it is 0.0, we want 0.
043    * This scales the fractional part onto the range [0-255]. That value
044    * is then used to construct a new color with which we can set the
045    * color of the sprite. The new color has an "alpha channel", a
046    * transparency value. So it is just like the R, G, or B channels,
047    * just used to determine how opaque the color is (higher number =
048    * more opaque).
049    */
050   private void updateDisplay() {
051     int floorSeconds = (intremainingTime;// whole number below time
052     double fractionalSeconds = remainingTime - floorSeconds;
053     int ceilingSeconds = floorSeconds + 1;// whole number above time
054     int scaledAlpha = (int) (256 * fractionalSeconds);
055     setColor(Palette.getColor(getColor(), scaledAlpha));
056     setText(Integer.toString(ceilingSeconds));
057   }
058 
059   /**
060    * Return the number of seconds remaining in this countdown.
061    *
062    @return  seconds remaining; note that it can be negative
063    */
064   public double getRemainingTime() {
065     return remainingTime;
066   }
067 
068   /**
069    * Has the count expired?
070    *
071    @return  true if we are no longer counting down, false othewise
072    */
073   public boolean isExpired() {
074     return remainingTime <= 0.0;
075   }
076 
077   /**
078    Set the countdown timer to the give value. Value will be the
079    * maximum of given and 0.0 (since a negative timer makes no sense)
080    *
081    @param  remainingTime  seconds from which to count down
082    */
083   public void setTimer(double remainingTime{
084     this.remainingTime = Math.max(0.0, remainingTime);
085   }
086 
087   /**
088    * Activate the countdown timer.
089    */
090   public void startTimer() {
091     show();
092     updateDisplay();
093   }
094 
095   /**
096    * Advance the countdown timer one tick. If the remaining time goes to
097    * or below 0.0, will call {@code finishCountdown}; otherwise calls
098    {@code updateDisplay}.
099    *
100    @param  deltaT  time since last call to advance (in seconds)
101    */
102   public void advance(double deltaT{
103     if (!isExpired()) {
104       remainingTime -= deltaT;
105       if (remainingTime <= 0.0{
106         finishCountdown();
107       } else {
108         updateDisplay();
109       }
110     }
111   }
112 }
113 
114 //Uploaded on Mon Mar 29 21:40:29 EDT 2010


Download/View scg/ch06/CountdownTimer.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games