From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch06;
002
003 importimport means to make the classes and/or packages available in this program fang2.attributes.Palette;
004 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
005
006 /**
007 * Countdown timer: A {open braces start code blocks and must be matched with a close brace@link fang2.sprites.StringSprite StringSprite}close braces end code blocks and must match an earlier open brace
008 * -derived classclass is a group of fields and methods used for making objects which counts down from a given time.
009 */
010 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects CountdownTimer
011 extendsextends means to customize or extend the functionality of a class StringSprite {open braces start code blocks and must be matched with a close brace
012 /** the time remaining to be counted down in seconds */
013 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions remainingTime;
014
015 /**
016 * Construct newnew is used to create objects by calling the constructor timer. Timer set to 0.0 seconds and then turned off.
017 */
018 publicpublic is used to indicate unrestricted access (any other class can have access) CountdownTimer() {open braces start code blocks and must be matched with a close brace
019 remainingTime =this assignment operator makes the left side equal to the right side 0.0;
020 hide();// make the timer invisible
021 }close braces end code blocks and must match an earlier open brace
022
023 /**
024 * This method is called once from {open braces start code blocks and must be matched with a close brace@code advance}close braces end code blocks and must match an earlier open brace 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 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value finishCountdown() {open braces start code blocks and must be matched with a close brace
030 hide();
031 }close braces end code blocks and must match an earlier open brace
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 (intint is the type for whole numbers and it is short for integer) 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 =this assignment operator makes the left side equal to the right side 2.24 implies (intint is the type for whole numbers and it is short for integer)
040 * remainingTime =this assignment operator makes the left side equal to the right side 2 and remainingTime - (intint is the type for whole numbers and it is short for integer) remainingTime =this assignment operator makes the left side equal to the right side 0.24).
041 * We need the fraction converted to a value between 0 and 255: ifif executes the next statement only if the condition in parenthesis evaluates to true the
042 * fractional part is 0.5, we want 128 and ifif executes the next statement only if the condition in parenthesis evaluates to true it is 0.0, we want 0.
043 * This scales the fractional part onto the range [brackets are typically used to declare, initialize and index (indicate which element of) arrays0-255]brackets are typically used to declare, initialize and index (indicate which element of) arrays. That value
044 * is then used to construct a newnew is used to create objects by calling the constructor color with which we can set the
045 * color of the sprite. The newnew is used to create objects by calling the constructor 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 =this assignment operator makes the left side equal to the right side
048 * more opaque).
049 */
050 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value updateDisplay() {open braces start code blocks and must be matched with a close brace
051 intint is the type for whole numbers and it is short for integer floorSeconds =this assignment operator makes the left side equal to the right side (intint is the type for whole numbers and it is short for integer) remainingTime;// whole number below time
052 doubledouble is the type for numbers that can contain decimal fractions fractionalSeconds =this assignment operator makes the left side equal to the right side remainingTime - floorSeconds;
053 intint is the type for whole numbers and it is short for integer ceilingSeconds =this assignment operator makes the left side equal to the right side floorSeconds +adds two numbers together or concatenates Strings together 1;// whole number above time
054 intint is the type for whole numbers and it is short for integer scaledAlpha =this assignment operator makes the left side equal to the right side (intint is the type for whole numbers and it is short for integer) (256 * fractionalSeconds);
055 setColor(Palette.getColor(getColor(), scaledAlpha));
056 setText(Integer.toString(ceilingSeconds));
057 }close braces end code blocks and must match an earlier open brace
058
059 /**
060 * Return the number of seconds remaining in thisthis means the current object (the implicit parameter) countdown.
061 *
062 * @returnnull seconds remaining; note that it can be negative
063 */
064 publicpublic is used to indicate unrestricted access (any other class can have access) doubledouble is the type for numbers that can contain decimal fractions getRemainingTime() {open braces start code blocks and must be matched with a close brace
065 returnreturn means to provide the result of the method and/or cease execution of the method immediately remainingTime;
066 }close braces end code blocks and must match an earlier open brace
067
068 /**
069 * Has the count expired?
070 *
071 * @returnnull truetrue is the boolean value that is the opposite of false ifif executes the next statement only if the condition in parenthesis evaluates to true we are no longer counting down, falsefalse is a value for the boolean type and means not true othewise
072 */
073 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isExpired() {open braces start code blocks and must be matched with a close brace
074 returnreturn means to provide the result of the method and/or cease execution of the method immediately remainingTime <=this evaluates to true if the left side is not more than the right side 0.0;
075 }close braces end code blocks and must match an earlier open brace
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 * @paramthis is the Javadoc tag for documenting the purpose of parameters remainingTime seconds from which to count down
082 */
083 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setTimer(doubledouble is the type for numbers that can contain decimal fractions remainingTime) {open braces start code blocks and must be matched with a close brace
084 thisthis means the current object (the implicit parameter).remainingTime =this assignment operator makes the left side equal to the right side Math.max(0.0, remainingTime);
085 }close braces end code blocks and must match an earlier open brace
086
087 /**
088 * Activate the countdown timer.
089 */
090 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startTimer() {open braces start code blocks and must be matched with a close brace
091 show();
092 updateDisplay();
093 }close braces end code blocks and must match an earlier open brace
094
095 /**
096 * Advance the countdown timer one tick. If the remaining time goes to
097 * or below 0.0, will call {open braces start code blocks and must be matched with a close brace@code finishCountdown}close braces end code blocks and must match an earlier open brace; otherwise calls
098 * {open braces start code blocks and must be matched with a close brace@code updateDisplay}close braces end code blocks and must match an earlier open brace.
099 *
100 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaT time since last call to advance (in seconds)
101 */
102 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value advance(doubledouble is the type for numbers that can contain decimal fractions deltaT) {open braces start code blocks and must be matched with a close brace
103 ifif executes the next statement only if the condition in parenthesis evaluates to true (!this is the not operator, which changes true to false and false to trueisExpired()) {open braces start code blocks and must be matched with a close brace
104 remainingTime -=this decreases the variable on the left by the value on the right deltaT;
105 ifif executes the next statement only if the condition in parenthesis evaluates to true (remainingTime <=this evaluates to true if the left side is not more than the right side 0.0) {open braces start code blocks and must be matched with a close brace
106 finishCountdown();
107 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
108 updateDisplay();
109 }close braces end code blocks and must match an earlier open brace
110 }close braces end code blocks and must match an earlier open brace
111 }close braces end code blocks and must match an earlier open brace
112 }close braces end code blocks and must match an earlier open brace
113
114 //Uploaded on Mon Mar 29 21:40:29 EDT 2010
|
Download/View scg/ch06/CountdownTimer.java