From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch07;
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.Location2D;
006 importimport means to make the classes and/or packages available in this program fang2.core.Game;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.CompositeSprite;
008 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
009 importimport means to make the classes and/or packages available in this program fang2.sprites.StringSprite;
010
011 /**
012 * GameTile classclass is a group of fields and methods used for making objects. Provides a tile forfor is a looping structure for repeatedly executing a block of code a game of Tic-Tac-Toe (or any
013 * similar game). The tile can display a single character in front of a
014 * solid background. The tile can also report what their content is,
015 * whether or not they are occupied, and whether or not they have been
016 * clicked on.
017 */
018 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 GameTile
019 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
020 /** the constant (finalfinal means not changeable (often used for constants)), classclass is a group of fields and methods used for making objects-wide (staticstatic means that an instance is not required for access (class level access)) Color forfor is a looping structure for repeatedly executing a block of code the tile */
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) Color DEFAULT_COLOR =this assignment operator makes the left side equal to the right side
022 Game.getColor("dark violet");
023
024 /** the constant (finalfinal means not changeable (often used for constants)), classclass is a group of fields and methods used for making objects-wide (staticstatic means that an instance is not required for access (class level access)) Color forfor is a looping structure for repeatedly executing a block of code the text */
025 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_CONTENT_COLOR =this assignment operator makes the left side equal to the right side
026 Game.getColor("lavender");
027
028 /** The background of the tile */
029 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite background;
030
031 /** Current content of thisthis means the current object (the implicit parameter) tile */
032 privateprivate is used to restrict access to the current class only String content;
033
034 /** the visible content of the tile */
035 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) StringSprite displayContent;
036
037 /**
038 * Construct a newnew is used to create objects by calling the constructor GameTile: content =this assignment operator makes the left side equal to the right side ""
039 */
040 publicpublic is used to indicate unrestricted access (any other class can have access) GameTile() {open braces start code blocks and must be matched with a close brace
041 background =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(1.0, 1.0);
042 background.setColor(DEFAULT_COLOR);
043 addSprite(background);
044 content =this assignment operator makes the left side equal to the right side "";
045 displayContent =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringSprite();
046 displayContent.setLocation(0.00, 0.075);
047 displayContent.setColor(DEFAULT_CONTENT_COLOR);
048 addSprite(displayContent);
049 }close braces end code blocks and must match an earlier open brace
050
051 /**
052 * Get the current content of the tile.
053 *
054 * @returnnull the content
055 */
056 publicpublic is used to indicate unrestricted access (any other class can have access) String getContent() {open braces start code blocks and must be matched with a close brace
057 returnreturn means to provide the result of the method and/or cease execution of the method immediately content;
058 }close braces end code blocks and must match an earlier open brace
059
060 /**
061 * Is thisthis means the current object (the implicit parameter) tile occupied by one of the players. A tile is clear ifif executes the next statement only if the condition in parenthesis evaluates to true the
062 * content string is empty, occupied ifif executes the next statement only if the condition in parenthesis evaluates to true it has any non-empty value.
063 *
064 * @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 tile is occupied; falsefalse is a value for the boolean type and means not true otherwise.
065 */
066 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isOccupied() {open braces start code blocks and must be matched with a close brace
067 returnreturn means to provide the result of the method and/or cease execution of the method immediately content.length() !=this is the not equals operator which evaluates to true if both sides are different 0;
068 }close braces end code blocks and must match an earlier open brace
069
070 /**
071 * Was the mouse belonging to the given player just clicked inside of
072 * thisthis means the current object (the implicit parameter) tile? Uses currentGame to read the input (using getClick2D).
073 *
074 * @paramthis is the Javadoc tag for documenting the purpose of parameters playerID the id number of player whose turn it is.
075 *
076 * @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 mouse clicked within the background, falsefalse is a value for the boolean type and means not true
077 * otherwise.
078 */
079 publicpublic is used to indicate unrestricted access (any other class can have access) booleanboolean is a type that is either true or false isPressed(intint is the type for whole numbers and it is short for integer playerID) {open braces start code blocks and must be matched with a close brace
080 // The current game may have a mouse click or not.
081 Location2D mouseClick =this assignment operator makes the left side equal to the right side Game.getCurrentGame().getClick2D(playerID);
082 ifif executes the next statement only if the condition in parenthesis evaluates to true (mouseClick !=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) {open braces start code blocks and must be matched with a close brace
083 ifif executes the next statement only if the condition in parenthesis evaluates to true (intersects(mouseClick)) {open braces start code blocks and must be matched with a close brace
084 returnreturn means to provide the result of the method and/or cease execution of the method immediately truetrue is the boolean value that is the opposite of false;
085 }close braces end code blocks and must match an earlier open brace
086 }close braces end code blocks and must match an earlier open brace
087 returnreturn means to provide the result of the method and/or cease execution of the method immediately falsefalse is a value for the boolean type and means not true;
088 }close braces end code blocks and must match an earlier open brace
089
090 /**
091 * Set the color of the background rectangle
092 *
093 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set the rectangle to.
094 */
095 @Override
096 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
097 background.setColor(color);
098 }close braces end code blocks and must match an earlier open brace
099
100 /**
101 * Set the tile content to the given value. A single character is
102 * expected; ifif executes the next statement only if the condition in parenthesis evaluates to true a multi-character string is passed in, the content
103 * will be set to the first character in the string.
104 *
105 * @paramthis is the Javadoc tag for documenting the purpose of parameters content the newnew is used to create objects by calling the constructor content value
106 */
107 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setContent(String content) {open braces start code blocks and must be matched with a close brace
108 ifif executes the next statement only if the condition in parenthesis evaluates to true (content.length() > 1) {open braces start code blocks and must be matched with a close brace
109 content =this assignment operator makes the left side equal to the right side content.substring(0, 1);
110 }close braces end code blocks and must match an earlier open brace
111 thisthis means the current object (the implicit parameter).content =this assignment operator makes the left side equal to the right side content;
112 setText(content);
113 }close braces end code blocks and must match an earlier open brace
114
115 /**
116 * Set the color of the text on the background.
117 *
118 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set the text to
119 */
120 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setTextColor(Color color) {open braces start code blocks and must be matched with a close brace
121 displayContent.setColor(color);
122 }close braces end code blocks and must match an earlier open brace
123
124 /**
125 * Set text message; resize to fit in one line
126 *
127 * @paramthis is the Javadoc tag for documenting the purpose of parameters message the newnew is used to create objects by calling the constructor message forfor is a looping structure for repeatedly executing a block of code the background to display
128 */
129 privateprivate is used to restrict access to the current class only voidvoid means the method does not return a value setText(String message) {open braces start code blocks and must be matched with a close brace
130 displayContent.setText(message);
131 displayContent.setWidth(0.75);
132 }close braces end code blocks and must match an earlier open brace
133 }close braces end code blocks and must match an earlier open brace
134
135 //Uploaded on Mon Mar 29 21:42:29 EDT 2010
|
Download/View scg/ch07/GameTile.java