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.core.Game;
006 importimport means to make the classes and/or packages available in this program fang2.core.Sprite;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.LineSprite;
008 importimport means to make the classes and/or packages available in this program fang2.sprites.OvalSprite;
009
010 /**
011 * The "projectile" forfor is a looping structure for repeatedly executing a block of code the RescueMission game. The RescueRing is the
012 * ring thrown out to rescue a drowning victim in the water. The ring is
013 * a {open braces start code blocks and must be matched with a close brace@link fang2.sprites.CompositeSprite CompositeSprite}close braces end code blocks and must match an earlier open brace with two
014 * circles in it. The inner circle serves as the "hole" through a
015 * doughnut. It is not, in fact, transparent. The projectile also keeps
016 * track of the rescue rope between itself and the rescuer. This is
017 * simply a {open braces start code blocks and must be matched with a close brace@link fang2.sprites.LineSprite LineSprite}close braces end code blocks and must match an earlier open brace that is colored
018 * the color of a "rope". Each time the ring moves, the rope is updated
019 * to have end points at the {open braces start code blocks and must be matched with a close brace@link Rescuer}close braces end code blocks and must match an earlier open brace and the ring. A RescueRing
020 * is in one of two states: it is ready to be thrown or it is flying.
021 * The methods isReady and isFlying permit the state of the ring to be
022 * checked.
023 */
024 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 RescueRing
025 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
026 /**
027 * the defaultdefault is what is executed when no cases are matched color of the hole; use setBackgroundColor to change
028 * after ring is constructed.
029 */
030 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) finalfinal means not changeable (often used for constants) Color DEFAULT_BACKGROUND_COLOR =this assignment operator makes the left side equal to the right side Game.getColor(
031 "cyan");
032
033 /**
034 * the defaultdefault is what is executed when no cases are matched color of the ring itself; use setColor to change after
035 * ring is constructed
036 */
037 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) finalfinal means not changeable (often used for constants) Color DEFAULT_RING_COLOR =this assignment operator makes the left side equal to the right side Game.getColor(
038 "misty rose");
039
040 /**
041 * the defaultdefault is what is executed when no cases are matched color of the rope; use setRopeColor to change after
042 * ring is constructed
043 */
044 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) finalfinal means not changeable (often used for constants) Color DEFAULT_ROPE_COLOR =this assignment operator makes the left side equal to the right side Game.getColor(
045 "goldenrod");
046
047 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) OvalSprite inner;
048
049 /** outer/inner are the appearance of the ring. inner is the hole */
050 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) OvalSprite outer;
051
052 /** is the ring ready forfor is a looping structure for repeatedly executing a block of code launch? */
053 privateprivate is used to restrict access to the current class only booleanboolean is a type that is either true or false readyForLaunch;
054
055 /**
056 * the rescuer with which thisthis means the current object (the implicit parameter) ring is associated; the rope goes back
057 * to the rescuer _and_ the ring returns to the rescuer when it goes
058 * off the screen or rescues an individual
059 */
060 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) Rescuer rescuer;
061
062 /** the rope connecting the ring to the rescuer */
063 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) LineSprite rescueRope;
064
065 /**
066 * Construct a brand newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@code RescueRing}close braces end code blocks and must match an earlier open brace. The inner/outer ring are
067 * part of the {open braces start code blocks and must be matched with a close brace@link fang2.sprite.CompositeSprite CompositeSprite}close braces end code blocks and must match an earlier open brace
068 * whilewhile is a looping structure for executing code repeatedly the rope is outside of the ring (so that it can connect the
069 * location of the {open braces start code blocks and must be matched with a close brace@link Rescuer}close braces end code blocks and must match an earlier open brace and the {open braces start code blocks and must be matched with a close brace@code RescueRing}close braces end code blocks and must match an earlier open brace.
070 *
071 * @paramthis is the Javadoc tag for documenting the purpose of parameters rescuer the {open braces start code blocks and must be matched with a close brace@link Rescuer}close braces end code blocks and must match an earlier open brace with which thisthis means the current object (the implicit parameter) ring is
072 * associated
073 */
074 publicpublic is used to indicate unrestricted access (any other class can have access) RescueRing(Rescuer rescuer, 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
075 thisthis means the current object (the implicit parameter).rescuer =this assignment operator makes the left side equal to the right side rescuer;
076
077 outer =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor OvalSprite(1, 1);
078 outer.setColor(DEFAULT_RING_COLOR);
079 inner =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);
080 inner.setColor(DEFAULT_BACKGROUND_COLOR);
081 addSprite(outer);
082 addSprite(inner);
083
084 rescueRope =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor LineSprite();
085 rescueRope.setColor(DEFAULT_ROPE_COLOR);
086 rescueRope.hide();// make it invisible (only visible after launch)
087
088 setVelocity(deltaX, deltaY);
089
090 startReady();
091 }close braces end code blocks and must match an earlier open brace
092
093 /**
094 * Advance the rescue ring one frame. If flying, move according to
095 * velocity and update rope's end points; otherwise dodo is part of the do-while looping structure (post condition loop) nothing.
096 *
097 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT seconds elapsed since last frame
098 */
099 @Override
100 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
101 ifif executes the next statement only if the condition in parenthesis evaluates to true (isFlying()) {open braces start code blocks and must be matched with a close brace
102 super.advance(dT);
103 rescueRope.setStart(rescuer.getLocation());
104 rescueRope.setEnd(getLocation());
105 }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
106 setLocation(rescuer.getLocation());
107 }close braces end code blocks and must match an earlier open brace
108 }close braces end code blocks and must match an earlier open brace
109
110 /**
111 * Finish flying when the ring moves off 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 (isFlying() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) runIntoAnyWall()) {open braces start code blocks and must be matched with a close brace
116 startReady();
117 }close braces end code blocks and must match an earlier open brace
118 }close braces end code blocks and must match an earlier open brace
119
120 /**
121 * Return the rope connecting the ring to the rescuer
122 *
123 * @returnnull reference to the rope used to connect thisthis means the current object (the implicit parameter) ring to the
124 * rescuer
125 */
126 publicpublic is used to indicate unrestricted access (any other class can have access) LineSprite getRope() {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 rescueRope;
128 }close braces end code blocks and must match an earlier open brace
129
130 /**
131 * The ring is either ready or flying; returns 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 ring is not
132 * ready but rather in flight.
133 *
134 * @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 ring is not ready; falsefalse is a value for the boolean type and means not true otherwise
135 */
136 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isFlying() {open braces start code blocks and must be matched with a close brace
137 returnreturn means to provide the result of the method and/or cease execution of the method immediately !this is the not operator, which changes true to false and false to truereadyForLaunch;
138 }close braces end code blocks and must match an earlier open brace
139
140 /**
141 * Is the ring in the ready state? Is it ready to be thrown?
142 *
143 * @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 ring is ready, falsefalse is a value for the boolean type and means not true otherwise
144 */
145 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isReady() {open braces start code blocks and must be matched with a close brace
146 returnreturn means to provide the result of the method and/or cease execution of the method immediately readyForLaunch;
147 }close braces end code blocks and must match an earlier open brace
148
149 /**
150 * Is the rescue ring in the process of rescuing a given victim? If
151 * the victim is not nullnull is the value used to refer to a non-existant object then an intersection test is done; ifif executes the next statement only if the condition in parenthesis evaluates to true thisthis means the current object (the implicit parameter)
152 * ring intersects the victim, then the victim is being rescued and
153 * thisthis means the current object (the implicit parameter) method returns truetrue is the boolean value that is the opposite of false.
154 *
155 * @paramthis is the Javadoc tag for documenting the purpose of parameters victim the sprite we _might_ be rescuing
156 *
157 * @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 ring is flying and victim is non-nullnull is the value used to refer to a non-existant object and ring
158 * intersects the victim; falsefalse is a value for the boolean type and means not true otherwise
159 */
160 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isRescuing(Sprite victim) {open braces start code blocks and must be matched with a close brace
161 returnreturn means to provide the result of the method and/or cease execution of the method immediately isFlying() &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) (victim !=this is the not equals operator which evaluates to true if both sides are different nullnull is the value used to refer to a non-existant object) &this performs a bit-wise and (not the same as boolean and which is &&)&this performs a bit-wise and (not the same as boolean and which is &&) intersects(victim);
162 }close braces end code blocks and must match an earlier open brace
163
164 /**
165 * Set the color of the inner ring.
166 *
167 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set
168 */
169 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setBackgroundColor(Color color) {open braces start code blocks and must be matched with a close brace
170 inner.setColor(color);
171 }close braces end code blocks and must match an earlier open brace
172
173 /**
174 * Set the color of the outer ring.
175 *
176 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set the outer ring
177 */
178 @Override
179 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
180 outer.setColor(color);
181 }close braces end code blocks and must match an earlier open brace
182
183 /**
184 * Set the color of the rope
185 *
186 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color of the rope to set
187 */
188 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setRopeColor(Color color) {open braces start code blocks and must be matched with a close brace
189 rescueRope.setColor(color);
190 }close braces end code blocks and must match an earlier open brace
191
192 /**
193 * Move from ready to flying state.
194 */
195 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startFlying() {open braces start code blocks and must be matched with a close brace
196 ifif executes the next statement only if the condition in parenthesis evaluates to true (isReady()) {open braces start code blocks and must be matched with a close brace
197 readyForLaunch =this assignment operator makes the left side equal to the right side falsefalse is a value for the boolean type and means not true;
198 rescueRope.show();
199 rescueRope.setStart(rescuer.getLocation());
200 rescueRope.setEnd(getLocation());
201 }close braces end code blocks and must match an earlier open brace
202 }close braces end code blocks and must match an earlier open brace
203
204 /**
205 * Move from flying to ready state.
206 */
207 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value startReady() {open braces start code blocks and must be matched with a close brace
208 ifif executes the next statement only if the condition in parenthesis evaluates to true (isFlying()) {open braces start code blocks and must be matched with a close brace
209 readyForLaunch =this assignment operator makes the left side equal to the right side truetrue is the boolean value that is the opposite of false;
210 rescueRope.hide();
211 setLocation(rescuer.getLocation());
212 }close braces end code blocks and must match an earlier open brace
213 }close braces end code blocks and must match an earlier open brace
214 }close braces end code blocks and must match an earlier open brace
215
216 //Uploaded on Mon Mar 29 21:41:12 EDT 2010
|
Download/View scg/ch09/RescueRing.java