From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch09;
002
003 importimport means to make the classes and/or packages available in this program java.awt.Color;
004
005 importimport means to make the classes and/or packages available in this program fang2.attributes.Palette;
006 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
008
009 /**
010 * The characters that need to be rescued. These folk will wave their
011 * "arms" as they move back and forth across the screen.
012 */
013 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 Swimmer
014 extendsextends means to customize or extend the functionality of a class SpriteWithVelocity {open braces start code blocks and must be matched with a close brace
015 /** shared bump flag: NOT finalfinal means not changeable (often used for constants) */
016 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) booleanboolean is a type that is either true or false bumpedTheWall =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
017
018 /** Number of different animation frames */
019 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer FRAME_COUNT =this assignment operator makes the left side equal to the right side 4;
020
021 /** Offsets forfor is a looping structure for repeatedly executing a block of code the left and right arm, respectively */
022 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions leftArmX =this assignment operator makes the left side equal to the right side -0.12;
023 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions leftArmY =this assignment operator makes the left side equal to the right side -0.25;
024
025 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions rightArmX =this assignment operator makes the left side equal to the right side 0.12;
026 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions rightArmY =this assignment operator makes the left side equal to the right side -0.25;
027
028 /** How longlong is the type for whole numbers (they have a larger range than int) should each animation frame last? */
029 privateprivate is used to restrict access to the current class only staticstatic means that an instance is not required for access (class level access) finalfinal means not changeable (often used for constants) doubledouble is the type for numbers that can contain decimal fractions SECONDS_PER_FRAME =this assignment operator makes the left side equal to the right side 0.25;
030
031 /** Which animation frame is currently showing? */
032 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer frame;
033
034 /** the value of thisthis means the current object (the implicit parameter) swimmer ifif executes the next statement only if the condition in parenthesis evaluates to true rescued; immutable */
035 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) intint is the type for whole numbers and it is short for integer score;
036
037 /** The internal parts of the swimmer's body */
038 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) OvalSprite swimmerBody;
039
040 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite swimmerLeftArm;
041
042 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite swimmerRightArm;
043
044 /** animation timer; expires with call to timerExpried */
045 protectedprotected is used to restrict access to the current class and subclasses only doubledouble is the type for numbers that can contain decimal fractions timer;
046
047 /**
048 * Create a newnew is used to create objects by calling the constructor swimmer with given score and velocity.
049 *
050 * @paramthis is the Javadoc tag for documenting the purpose of parameters score the score forfor is a looping structure for repeatedly executing a block of code rescuing thisthis means the current object (the implicit parameter) swimmer
051 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaX initial horizontal velocity (in screens/sec)
052 * @paramthis is the Javadoc tag for documenting the purpose of parameters deltaY initial vertical velocity (in screens/sec)
053 */
054 publicpublic is used to indicate unrestricted access (any other class can have access) Swimmer(intint is the type for whole numbers and it is short for integer score, doubledouble is the type for numbers that can contain decimal fractions deltaX, doubledouble is the type for numbers that can contain decimal fractions deltaY) {open braces start code blocks and must be matched with a close brace
055 super(deltaX, deltaY);
056 thisthis means the current object (the implicit parameter).score =this assignment operator makes the left side equal to the right side score;
057 swimmerRightArm =this assignment operator makes the left side equal to the right side makeArm(rightArmX, rightArmY);
058 swimmerLeftArm =this assignment operator makes the left side equal to the right side makeArm(leftArmX, leftArmY);
059 swimmerBody =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(0.5, 0.5);
060 swimmerBody.setLocation(0.0, 0.0);
061 addSprite(swimmerBody);
062
063 frame =this assignment operator makes the left side equal to the right side 0;// setFrame(int) rotates the sprite
064 startTimer();
065 }close braces end code blocks and must match an earlier open brace
066
067 /**
068 * Called when some swimmer bumps the wall. This is staticstatic means that an instance is not required for access (class level access) so that all
069 * swimmers share the same flag (so all bounce as one unit)
070 */
071 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value bumpTheWall() {open braces start code blocks and must be matched with a close brace
072 bumpedTheWall =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
073 }close braces end code blocks and must match an earlier open brace
074
075 /**
076 * Called when bumping the wall is "serviced". This makes sure that
077 * the swimmers are ready to bounce off the next wall.
078 */
079 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) voidvoid means the method does not return a value clearBumpTheWall() {open braces start code blocks and must be matched with a close brace
080 bumpedTheWall =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
081 }close braces end code blocks and must match an earlier open brace
082
083 /**
084 * Get the bumped-the-wall flag.
085 *
086 * @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 the wall was bumped (and not cleared); falsefalse is a value for the boolean type and means not true
087 * otherwise
088 */
089 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) booleanboolean is a type that is either true or false hasBumpedTheWall() {open braces start code blocks and must be matched with a close brace
090 returnreturn means to provide the result of the method and/or cease execution of the method immediately bumpedTheWall;
091 }close braces end code blocks and must match an earlier open brace
092
093 /**
094 * Move swimmer and set a wall-hit flag ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter) swimmer has touched a
095 * wall. Uses super.advance(doubledouble is the type for numbers that can contain decimal fractions) and decrementTimer(doubledouble is the type for numbers that can contain decimal fractions): ALL
096 * subclasses of Swimmer must call decrementTimer in advance.
097 *
098 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT seconds since the last frame update
099 */
100 @Override
101 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 dT) {open braces start code blocks and must be matched with a close brace
102 super.advance(dT);
103 decrementTimer(dT);
104 ifif executes the next statement only if the condition in parenthesis evaluates to true (runIntoLeftWall() ||this is boolean or, meaning if either or both are true then the result is true runIntoRightWall()) {open braces start code blocks and must be matched with a close brace
105 bumpTheWall();
106 }close braces end code blocks and must match an earlier open brace
107 }close braces end code blocks and must match an earlier open brace
108
109 /**
110 * Bounce thisthis means the current object (the implicit parameter) swimmer off the wall. If any swimmer has struck the
111 * wall, reverse movement and move down the screen.
112 */
113 @Override
114 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value bounceOffEdges() {open braces start code blocks and must be matched with a close brace
115 ifif executes the next statement only if the condition in parenthesis evaluates to true (hasBumpedTheWall()) {open braces start code blocks and must be matched with a close brace
116 reverseVelocity();
117 setY(getY() +adds two numbers together or concatenates Strings together (getHeight() / 2));
118 }close braces end code blocks and must match an earlier open brace
119 }close braces end code blocks and must match an earlier open brace
120
121 /**
122 * Get the score value of thisthis means the current object (the implicit parameter) swimmer.
123 *
124 * @returnnull the initially set value of the score.
125 */
126 publicpublic is used to indicate unrestricted access (any other class can have access) intint is the type for whole numbers and it is short for integer getScore() {open braces start code blocks and must be matched with a close brace
127 returnreturn means to provide the result of the method and/or cease execution of the method immediately score;
128 }close braces end code blocks and must match an earlier open brace
129
130 /**
131 * Set the color of thisthis means the current object (the implicit parameter) sprite (actually the body of the sprite).
132 */
133 @Override
134 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setColor(Color color) {open braces start code blocks and must be matched with a close brace
135 swimmerBody.setColor(color);
136 }close braces end code blocks and must match an earlier open brace
137
138 /**
139 * Create a newnew is used to create objects by calling the constructor arm. Arms are rectangle sprites with a given offset
140 * and scale. This method is used to create one arm centered at the
141 * given coordinates.
142 *
143 * @paramthis is the Javadoc tag for documenting the purpose of parameters xCoordinate horizontal center (in CompositeSprite
144 * coordinates) of arm
145 * @paramthis is the Javadoc tag for documenting the purpose of parameters yCoordinate vertical center (in CompositeSprite
146 * coordinates) of arm
147 *
148 * @returnnull
149 */
150 privateprivate is used to restrict access to the current class only RectangleSprite makeArm(doubledouble is the type for numbers that can contain decimal fractions xCoordinate,
151 doubledouble is the type for numbers that can contain decimal fractions yCoordinate) {open braces start code blocks and must be matched with a close brace
152 RectangleSprite theArm =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(0.1, 0.25);
153 theArm.setLocation(xCoordinate, yCoordinate);
154 theArm.setColor(Palette.getColor("goldenrod"));
155 addSprite(theArm);
156 returnreturn means to provide the result of the method and/or cease execution of the method immediately theArm;
157 }close braces end code blocks and must match an earlier open brace
158
159 /**
160 * Decrement the timer; thisthis means the current object (the implicit parameter) timer is used forfor is a looping structure for repeatedly executing a block of code handling animation
161 *
162 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT seconds since the last frame update
163 */
164 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value decrementTimer(doubledouble is the type for numbers that can contain decimal fractions dT) {open braces start code blocks and must be matched with a close brace
165 timer -=this decreases the variable on the left by the value on the right dT;
166 ifif executes the next statement only if the condition in parenthesis evaluates to true (timer <=this evaluates to true if the left side is not more than the right side 0) {open braces start code blocks and must be matched with a close brace
167 timerExpired();
168 startTimer();
169 }close braces end code blocks and must match an earlier open brace
170 }close braces end code blocks and must match an earlier open brace
171
172 /**
173 * Return the length of time the timer should use.
174 *
175 * @returnnull seconds before the timer should expire.
176 */
177 protectedprotected is used to restrict access to the current class and subclasses only doubledouble is the type for numbers that can contain decimal fractions getTimerCountdownTime() {open braces start code blocks and must be matched with a close brace
178 returnreturn means to provide the result of the method and/or cease execution of the method immediately SECONDS_PER_FRAME;
179 }close braces end code blocks and must match an earlier open brace
180
181 /**
182 * Set the animation frame number to the given frame number. Updates
183 * the position of the sprite to match the given frame number
184 *
185 * @paramthis is the Javadoc tag for documenting the purpose of parameters frameNumber the newnew is used to create objects by calling the constructor frame number; will be mapped onto
186 * [brackets are typically used to declare, initialize and index (indicate which element of) arrays0-FRAME_COUNT)
187 */
188 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value setFrame(intint is the type for whole numbers and it is short for integer frameNumber) {open braces start code blocks and must be matched with a close brace
189 frame =this assignment operator makes the left side equal to the right side frameNumber %this divides the left side by the right side and evaluates to the remainder FRAME_COUNT;
190 ifif executes the next statement only if the condition in parenthesis evaluates to true (frame ==this is the comparison operator which evaluates to true if both sides are the same 0) {open braces start code blocks and must be matched with a close brace
191 rotate(20);
192 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (frame ==this is the comparison operator which evaluates to true if both sides are the same 1) {open braces start code blocks and must be matched with a close brace
193 rotate(20);
194 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (frame ==this is the comparison operator which evaluates to true if both sides are the same 2) {open braces start code blocks and must be matched with a close brace
195 rotate(-20);
196 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false ifif executes the next statement only if the condition in parenthesis evaluates to true (frame ==this is the comparison operator which evaluates to true if both sides are the same 3) {open braces start code blocks and must be matched with a close brace
197 rotate(-20);
198 }close braces end code blocks and must match an earlier open brace
199 }close braces end code blocks and must match an earlier open brace
200
201 /**
202 * Set the timer's countdown value.
203 *
204 * @paramthis is the Javadoc tag for documenting the purpose of parameters timer seconds to count down
205 */
206 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value setTimer(doubledouble is the type for numbers that can contain decimal fractions timer) {open braces start code blocks and must be matched with a close brace
207 thisthis means the current object (the implicit parameter).timer =this assignment operator makes the left side equal to the right side timer;
208 }close braces end code blocks and must match an earlier open brace
209
210 /**
211 * Set the timer to the value given by getTimerCountdownTime.
212 */
213 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value startTimer() {open braces start code blocks and must be matched with a close brace
214 setTimer(getTimerCountdownTime());
215 }close braces end code blocks and must match an earlier open brace
216
217 /**
218 * What "happens" when the timer expires. This one just sets the frame
219 * to the next frame, updating the animation.
220 */
221 protectedprotected is used to restrict access to the current class and subclasses only voidvoid means the method does not return a value timerExpired() {open braces start code blocks and must be matched with a close brace
222 setFrame((frame +adds two numbers together or concatenates Strings together 1) %this divides the left side by the right side and evaluates to the remainder FRAME_COUNT);
223 }close braces end code blocks and must match an earlier open brace
224 }close braces end code blocks and must match an earlier open brace
225
226 //Uploaded on Mon Mar 29 21:39:58 EDT 2010
|
Download/View scg/ch09/Swimmer.java