From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch01.spacesprites;
02
03 importimport means to make the classes and/or packages available in this program java.awt.Color;
04
05 importimport means to make the classes and/or packages available in this program fang2.core.Game;
06 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
07
08 /**
09 * Keep and display score. The {open braces start code blocks and must be matched with a close brace@link ScoreSprite}close braces end code blocks and must match an earlier open brace is based on the
10 * version presented in Chapter09. The score in thisthis means the current object (the implicit parameter) version is an
11 * integer.
12 */
13 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 ScoreSprite
14 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
15 /** scale of the object on the screen */
16 publicpublic is used to indicate unrestricted access (any other class can have access) finalfinal means not changeable (often used for constants) staticstatic means that an instance is not required for access (class level access) doubledouble is the type for numbers that can contain decimal fractions DEFAULT_SCORE_SCALE =this assignment operator makes the left side equal to the right side 0.1;
17 /** color to use forfor is a looping structure for repeatedly executing a block of code the score */
18 publicpublic is used to indicate unrestricted access (any other class can have access) finalfinal means not changeable (often used for constants) staticstatic means that an instance is not required for access (class level access) Color DEFAULT_SCORE_COLOR
19 =this assignment operator makes the left side equal to the right side Game.getCurrentGame().getColor("yellow");
20
21 /** current score */
22 privateprivate is used to restrict access to the current class only intint is the type for whole numbers and it is short for integer score;
23
24 /**
25 * Default constructor: 0 is initial score.
26 */
27 publicpublic is used to indicate unrestricted access (any other class can have access) ScoreSprite() {open braces start code blocks and must be matched with a close brace
28 thisthis means the current object (the implicit parameter)(0);
29 }close braces end code blocks and must match an earlier open brace
30
31 /**
32 * Create newnew is used to create objects by calling the constructor ScoreSprite with the given score.
33 *
34 * @paramthis is the Javadoc tag for documenting the purpose of parameters score initial score
35 */
36 publicpublic is used to indicate unrestricted access (any other class can have access) ScoreSprite(intint is the type for whole numbers and it is short for integer score) {open braces start code blocks and must be matched with a close brace
37 setScore(score);
38 setScale(DEFAULT_SCORE_SCALE);
39 setColor(DEFAULT_SCORE_COLOR);
40 setLocation(getScale()/2, getScale()/2);
41 }close braces end code blocks and must match an earlier open brace
42
43 /**
44 * Implementation method: display the currently recorded score.
45 */
46 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value fixTextDisplay() {open braces start code blocks and must be matched with a close brace
47 setText(Integer.toString(score));
48 }close braces end code blocks and must match an earlier open brace
49
50 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value decrement() {open braces start code blocks and must be matched with a close brace
51 decrement(1);
52 }close braces end code blocks and must match an earlier open brace
53
54 /**
55 * Decrement score by n
56 *
57 * @paramthis is the Javadoc tag for documenting the purpose of parameters n number to subtract from score.
58 */
59 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value decrement(intint is the type for whole numbers and it is short for integer n) {open braces start code blocks and must be matched with a close brace
60 increment(-n);
61 }close braces end code blocks and must match an earlier open brace
62
63 /**
64 * Get the current score.
65 *
66 * @returnnull the current score
67 */
68 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
69 returnreturn means to provide the result of the method and/or cease execution of the method immediately score;
70 }close braces end code blocks and must match an earlier open brace
71
72 /**
73 * Increment the score by 1.
74 */
75 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value increment() {open braces start code blocks and must be matched with a close brace
76 increment(1);
77 }close braces end code blocks and must match an earlier open brace
78
79 /**
80 * Increment the score by the given value.
81 *
82 * @paramthis is the Javadoc tag for documenting the purpose of parameters n number to add to the current score
83 */
84 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value increment(intint is the type for whole numbers and it is short for integer n) {open braces start code blocks and must be matched with a close brace
85 setScore(score +adds two numbers together or concatenates Strings together n);
86 }close braces end code blocks and must match an earlier open brace
87
88 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setScore(intint is the type for whole numbers and it is short for integer score) {open braces start code blocks and must be matched with a close brace
89 Game game =this assignment operator makes the left side equal to the right side Game.getCurrentGame();
90 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 truegame.containsSprite(thisthis means the current object (the implicit parameter))) game.addSprite(thisthis means the current object (the implicit parameter));
91 thisthis means the current object (the implicit parameter).score =this assignment operator makes the left side equal to the right side score;
92 fixTextDisplay();
93 }close braces end code blocks and must match an earlier open brace
94 }close braces end code blocks and must match an earlier open brace
95
96 //Uploaded on Mon Mar 29 21:41:40 EDT 2010
|
Download/View scg/ch01/spacesprites/ScoreSprite.java