|
001 packagepackage is used to name the directory or folder a class is in scg.ch13.highscore;
002
003 importimport means to make the classes and/or packages available in this program java.awt.Color;
004 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
005
006 importimport means to make the classes and/or packages available in this program fang2.sprites.CompositeSprite;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
008
009 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 ScrollingMessageBox
010 extendsextends means to customize or extend the functionality of a class CompositeSprite {open braces start code blocks and must be matched with a close brace
011 /** extra lines between last item and first item again */
012 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) intint is the type for whole numbers and it is short for integer DEFAULT_GAP =this assignment operator makes the left side equal to the right side 1;
013
014 /** vertical distance between adjacent messages; internal screens */
015 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) doubledouble is the type for numbers that can contain decimal fractions DEFAULT_LINE_HEIGHT =this assignment operator makes the left side equal to the right side 0.2;
016
017 /** speed, screens/second, of scrolling (ifif executes the next statement only if the condition in parenthesis evaluates to true no other value set) */
018 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) doubledouble is the type for numbers that can contain decimal fractions DEFAULT_VELOCITY =this assignment operator makes the left side equal to the right side -0.25;
019
020 /** coordinate, in sprite coordinate screens, top edge of messages */
021 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) doubledouble is the type for numbers that can contain decimal fractions TOP_EDGE =this assignment operator makes the left side equal to the right side -0.5;
022
023 /** gap between last entry and first entry when cycling; in lines */
024 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer gap;
025
026 /** the index of the top (on the screen) display StringSprite */
027 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer indexOfTopLineOnScreen;
028
029 /** the distance between the lines, in sprite screens */
030 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions lineHeight;
031
032 /** the displayed list of StringSprite */
033 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) ArrayList<StringSprite> messages;
034
035 /** scrolling speed in screens/second */
036 privateprivate is used to restrict access to the current class only doubledouble is the type for numbers that can contain decimal fractions velocity;
037
038 /**
039 * Create a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link ScrollingMessageBox}close braces end code blocks and must match an earlier open brace with all defaultdefault is what is executed when no cases are matched values.
040 */
041 publicpublic is used to indicate unrestricted access (any other class can have access) ScrollingMessageBox() {open braces start code blocks and must be matched with a close brace
042 thisthis means the current object (the implicit parameter)(newnew is used to create objects by calling the constructor ArrayList<String>(), DEFAULT_VELOCITY, DEFAULT_GAP);
043 }close braces end code blocks and must match an earlier open brace
044
045 /**
046 * Create newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link ScrollingMessageBox}close braces end code blocks and must match an earlier open brace with given strings in it.
047 * The scrolling velocity and gap are also set here.
048 *
049 * @paramthis is the Javadoc tag for documenting the purpose of parameters messageStrings list of strings to display
050 * @paramthis is the Javadoc tag for documenting the purpose of parameters velocity scrolling velocity in screens/second
051 * @paramthis is the Javadoc tag for documenting the purpose of parameters gap distance, in lines, between last and first
052 * entries cycling
053 */
054 publicpublic is used to indicate unrestricted access (any other class can have access) ScrollingMessageBox(ArrayList<String> messageStrings,
055 doubledouble is the type for numbers that can contain decimal fractions velocity, intint is the type for whole numbers and it is short for integer gap) {open braces start code blocks and must be matched with a close brace
056 thisthis means the current object (the implicit parameter).velocity =this assignment operator makes the left side equal to the right side velocity;
057 thisthis means the current object (the implicit parameter).gap =this assignment operator makes the left side equal to the right side gap;
058 lineHeight =this assignment operator makes the left side equal to the right side DEFAULT_LINE_HEIGHT;
059
060 thisthis means the current object (the implicit parameter).messages =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<StringSprite>(messageStrings.size());
061 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different messageStrings.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
062 add(messageStrings.get(i));
063 }close braces end code blocks and must match an earlier open brace
064 }close braces end code blocks and must match an earlier open brace
065
066 /**
067 * Add the given string to the message list. Will fix up the location.
068 * Intended to be called before scrolling begins. Will not handle
069 * adding whilewhile is a looping structure for executing code repeatedly scrolling well.
070 *
071 * @paramthis is the Javadoc tag for documenting the purpose of parameters msg the string to add to the list of strings scrolling; a
072 * newnew is used to create objects by calling the constructor last string
073 */
074 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value add(String msg) {open braces start code blocks and must be matched with a close brace
075 StringSprite messageLine =this assignment operator makes the left side equal to the right side makeMessage(msg);
076 doubledouble is the type for numbers that can contain decimal fractions yCoordinate =this assignment operator makes the left side equal to the right side TOP_EDGE;
077 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 truemessages.isEmpty()) {open braces start code blocks and must be matched with a close brace
078 yCoordinate =this assignment operator makes the left side equal to the right side messages.get(messages.size() - 1).getY() +adds two numbers together or concatenates Strings together
079 lineHeight;
080 }close braces end code blocks and must match an earlier open brace
081 messageLine.setLocation(0.0, yCoordinate);
082 messages.add(messageLine);
083 addSprite(messageLine);
084 }close braces end code blocks and must match an earlier open brace
085
086 /**
087 * Scroll the displayed text up according to the speed of the
088 * animation.
089 *
090 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT time, in seconds, since last advance call
091 */
092 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
093 scrollMessages(dT);
094 wrapIfNecessary();
095 }close braces end code blocks and must match an earlier open brace
096
097 /**
098 * Get the current gap (in lines)
099 *
100 * @returnnull the gap
101 */
102 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 getGap() {open braces start code blocks and must be matched with a close brace
103 returnreturn means to provide the result of the method and/or cease execution of the method immediately gap;
104 }close braces end code blocks and must match an earlier open brace
105
106 /**
107 * Get the current line height (in sprite screens)
108 *
109 * @returnnull the lineHeight in internal coordinate screens
110 */
111 publicpublic is used to indicate unrestricted access (any other class can have access) doubledouble is the type for numbers that can contain decimal fractions getLineHeight() {open braces start code blocks and must be matched with a close brace
112 returnreturn means to provide the result of the method and/or cease execution of the method immediately lineHeight;
113 }close braces end code blocks and must match an earlier open brace
114
115 /**
116 * Get the scrolling velocity
117 *
118 * @returnnull the velocity in screens/second
119 */
120 publicpublic is used to indicate unrestricted access (any other class can have access) doubledouble is the type for numbers that can contain decimal fractions getVelocity() {open braces start code blocks and must be matched with a close brace
121 returnreturn means to provide the result of the method and/or cease execution of the method immediately velocity;
122 }close braces end code blocks and must match an earlier open brace
123
124 /**
125 * Pass the color down to the individual lines in the message window.
126 *
127 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set
128 */
129 @Override
130 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
131 super.setColor(color);// set the color
132 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different messages.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
133 messages.get(i).setColor(color);
134 }close braces end code blocks and must match an earlier open brace
135 }close braces end code blocks and must match an earlier open brace
136
137 /**
138 * Set the gap to the given value
139 *
140 * @paramthis is the Javadoc tag for documenting the purpose of parameters gap the gap to set negative numbers ignored.
141 */
142 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setGap(intint is the type for whole numbers and it is short for integer gap) {open braces start code blocks and must be matched with a close brace
143 ifif executes the next statement only if the condition in parenthesis evaluates to true (gap >=this evaluates to true if the left side is not less than the right side 0) {open braces start code blocks and must be matched with a close brace
144 thisthis means the current object (the implicit parameter).gap =this assignment operator makes the left side equal to the right side gap;
145 }close braces end code blocks and must match an earlier open brace
146 }close braces end code blocks and must match an earlier open brace
147
148 /**
149 * Set the distance between the lines; will take effect next wrap
150 * around the set
151 *
152 * @paramthis is the Javadoc tag for documenting the purpose of parameters lineHeight the lineHeight to set
153 */
154 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setLineHeight(doubledouble is the type for numbers that can contain decimal fractions lineHeight) {open braces start code blocks and must be matched with a close brace
155 thisthis means the current object (the implicit parameter).lineHeight =this assignment operator makes the left side equal to the right side lineHeight;
156 }close braces end code blocks and must match an earlier open brace
157
158 /**
159 * Set the scrolling velocity
160 *
161 * @paramthis is the Javadoc tag for documenting the purpose of parameters velocity newnew is used to create objects by calling the constructor velocity in screens/second
162 */
163 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setVelocity(doubledouble is the type for numbers that can contain decimal fractions velocity) {open braces start code blocks and must be matched with a close brace
164 thisthis means the current object (the implicit parameter).velocity =this assignment operator makes the left side equal to the right side velocity;
165 }close braces end code blocks and must match an earlier open brace
166
167 /**
168 * Create a newnew is used to create objects by calling the constructor message line in the sprite. Given the value, scale it
169 * to fit on the sprite, set color, and drive on
170 *
171 * @paramthis is the Javadoc tag for documenting the purpose of parameters msg the text of the newnew is used to create objects by calling the constructor line
172 *
173 * @returnnull a newnew is used to create objects by calling the constructor {open braces start code blocks and must be matched with a close brace@link StringSprite}close braces end code blocks and must match an earlier open brace with the given text and right
174 * scale and stuff
175 */
176 privateprivate is used to restrict access to the current class only StringSprite makeMessage(String msg) {open braces start code blocks and must be matched with a close brace
177 StringSprite messageLine =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
178 messageLine.setText(msg);
179 messageLine.setLineHeight(lineHeight * 0.8);
180 messageLine.centerJustify();
181 messageLine.setColor(getColor());
182 returnreturn means to provide the result of the method and/or cease execution of the method immediately messageLine;
183 }close braces end code blocks and must match an earlier open brace
184
185 /**
186 * Scroll the lines. Everybody moves according to the velocity.
187 *
188 * @paramthis is the Javadoc tag for documenting the purpose of parameters dT time, in seconds, since the last advance
189 */
190 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value scrollMessages(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
191 forfor is a looping structure for repeatedly executing a block of code (intint is the type for whole numbers and it is short for integer i =this assignment operator makes the left side equal to the right side 0; i !=this is the not equals operator which evaluates to true if both sides are different messages.size(); ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
192 messages.get(i).translateY(dT * velocity);
193 }close braces end code blocks and must match an earlier open brace
194 }close braces end code blocks and must match an earlier open brace
195
196 /**
197 * If top most line (on the screen) is too high (it has touched
198 * TOP_EDGE), move it down to the bottom of the scrolling list.
199 * indexOfTopScroller tracks topmost {open braces start code blocks and must be matched with a close brace@link StringSprite}close braces end code blocks and must match an earlier open brace.
200 */
201 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value wrapIfNecessary() {open braces start code blocks and must be matched with a close brace
202 StringSprite top =this assignment operator makes the left side equal to the right side messages.get(indexOfTopLineOnScreen);
203 ifif executes the next statement only if the condition in parenthesis evaluates to true (top.getMinY() < TOP_EDGE) {open braces start code blocks and must be matched with a close brace
204 intint is the type for whole numbers and it is short for integer indexOfBottomLineOnScreen =this assignment operator makes the left side equal to the right side (indexOfTopLineOnScreen +adds two numbers together or concatenates Strings together
205 (messages.size() - 1)) %this divides the left side by the right side and evaluates to the remainder messages.size();
206 StringSprite bottom =this assignment operator makes the left side equal to the right side messages.get(indexOfBottomLineOnScreen);
207 top.setY(bottom.getY() +adds two numbers together or concatenates Strings together lineHeight);// top just below bottom
208
209 ifif executes the next statement only if the condition in parenthesis evaluates to true ((indexOfTopLineOnScreen ==this is the comparison operator which evaluates to true if both sides are the same 0) &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 &&) (gap > 0)) {open braces start code blocks and must be matched with a close brace
210 top.translateY(lineHeight * gap);
211 }close braces end code blocks and must match an earlier open brace
212
213 indexOfTopLineOnScreen =this assignment operator makes the left side equal to the right side (indexOfTopLineOnScreen +adds two numbers together or concatenates Strings together 1) %this divides the left side by the right side and evaluates to the remainder
214 messages.size();
215 }close braces end code blocks and must match an earlier open brace
216 }close braces end code blocks and must match an earlier open brace
217 }close braces end code blocks and must match an earlier open brace
218
219 //Uploaded on Mon Mar 29 21:40:12 EDT 2010
|