|
001 packagepackage is used to name the directory or folder a class is in scg.ch13.core;
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.sprites.CompositeSprite;
007 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
008
009 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.I_Piece;
010 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.J_Piece;
011 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.L_Piece;
012 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.O_Piece;
013 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.Piece;
014 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.S_Piece;
015 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.T_Piece;
016 importimport means to make the classes and/or packages available in this program scg.ch13.pieces.Z_Piece;
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 NextPieceBox
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 /** background color swatch */
021 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite background;
022
023 /** outline color swatch */
024 privateprivate is used to restrict access to the current class only finalfinal means not changeable (often used for constants) RectangleSprite edges;
025
026 /** the current piece inside the box */
027 privateprivate is used to restrict access to the current class only Piece piece;
028
029 /**
030 * Make 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 NextPieceBox}close braces end code blocks and must match an earlier open brace with the given scale forfor is a looping structure for repeatedly executing a block of code the
031 * internal piece squares. Uses the same colors as defaulted in {open braces start code blocks and must be matched with a close brace@link
032 * Board}close braces end code blocks and must match an earlier open brace.
033 *
034 * @paramthis is the Javadoc tag for documenting the purpose of parameters squareEdgeSize size (in screens) of edge of one square in
035 * a piece as displayed
036 */
037 publicpublic is used to indicate unrestricted access (any other class can have access) NextPieceBox(doubledouble is the type for numbers that can contain decimal fractions squareEdgeSize) {open braces start code blocks and must be matched with a close brace
038 edges =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(1.4, 1.4);
039 setEdgeColor(Board.DEFAULT_EDGE_COLOR);
040 addSprite(edges);
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 setColor(Board.DEFAULT_BACKGROUND_COLOR);
043 addSprite(background);
044 setScale(squareEdgeSize * 5);
045 }close braces end code blocks and must match an earlier open brace
046
047 /**
048 * Get the current edge color
049 *
050 * @returnnull the current edge color
051 */
052 publicpublic is used to indicate unrestricted access (any other class can have access) Color getEdgeColor() {open braces start code blocks and must be matched with a close brace
053 returnreturn means to provide the result of the method and/or cease execution of the method immediately edges.getColor();
054 }close braces end code blocks and must match an earlier open brace
055
056 /**
057 * Get the current piece
058 *
059 * @returnnull reference to the {open braces start code blocks and must be matched with a close brace@link Piece}close braces end code blocks and must match an earlier open brace currently in the {open braces start code blocks and must be matched with a close brace@link
060 * NextPieceBox}close braces end code blocks and must match an earlier open brace.
061 */
062 publicpublic is used to indicate unrestricted access (any other class can have access) Piece getPiece() {open braces start code blocks and must be matched with a close brace
063 returnreturn means to provide the result of the method and/or cease execution of the method immediately piece;
064 }close braces end code blocks and must match an earlier open brace
065
066 /**
067 * Generate a newnew is used to create objects by calling the constructor piece. The piece is randomly created: it is one of 7
068 * pieces with one of 4 facings and has a random contrasting color.
069 */
070 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value nextPiece() {open braces start code blocks and must be matched with a close brace
071 // remove old piece from display
072 ifif executes the next statement only if the condition in parenthesis evaluates to true (piece !=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
073 removeSprite(piece);
074 }close braces end code blocks and must match an earlier open brace
075
076 // randomize piece, facing, and color
077 intint is the type for whole numbers and it is short for integer randomPiece =this assignment operator makes the left side equal to the right side Game.getCurrentGame().randomInt(
078 Piece.PIECE_COUNT);
079 intint is the type for whole numbers and it is short for integer facing =this assignment operator makes the left side equal to the right side Game.getCurrentGame().randomInt(4);
080 Color randomColor =this assignment operator makes the left side equal to the right side getRandomContrastingColor(getColor());
081
082 // the factory code; call the right constructor
083 ifif executes the next statement only if the condition in parenthesis evaluates to true (randomPiece ==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
084 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor I_Piece(facing, randomColor);
085 }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 (randomPiece ==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
086 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor L_Piece(facing, randomColor);
087 }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 (randomPiece ==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
088 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor J_Piece(facing, randomColor);
089 }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 (randomPiece ==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
090 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Z_Piece(facing, randomColor);
091 }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 (randomPiece ==this is the comparison operator which evaluates to true if both sides are the same 4) {open braces start code blocks and must be matched with a close brace
092 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor S_Piece(facing, randomColor);
093 }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 (randomPiece ==this is the comparison operator which evaluates to true if both sides are the same 5) {open braces start code blocks and must be matched with a close brace
094 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor O_Piece(facing, randomColor);
095 }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 (randomPiece ==this is the comparison operator which evaluates to true if both sides are the same 6) {open braces start code blocks and must be matched with a close brace
096 piece =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor T_Piece(facing, randomColor);
097 }close braces end code blocks and must match an earlier open brace
098 addSprite(piece);
099 }close braces end code blocks and must match an earlier open brace
100
101 /**
102 * Set the color (the background color) of thisthis means the current object (the implicit parameter) sprite. Caught here to
103 * set background, too.
104 *
105 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set
106 */
107 @Override
108 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
109 super.setColor(color);
110 background.setColor(color);
111 }close braces end code blocks and must match an earlier open brace
112
113 /**
114 * Set the color of edge, the edge box around the piece box.
115 *
116 * @paramthis is the Javadoc tag for documenting the purpose of parameters color the color to set
117 */
118 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setEdgeColor(Color color) {open braces start code blocks and must be matched with a close brace
119 edges.setColor(color);
120 }close braces end code blocks and must match an earlier open brace
121
122 /**
123 * Calculate average brightness of bgColor. If it is light, generate a
124 * dark color and ifif executes the next statement only if the condition in parenthesis evaluates to true it is dark, generate a light color. A random
125 * brightness is generated forfor is a looping structure for repeatedly executing a block of code each color, R, G, and B. It is in the
126 * low range [brackets are typically used to declare, initialize and index (indicate which element of) arrays0-128) to make dark colors or in the high range
127 * [brackets are typically used to declare, initialize and index (indicate which element of) arrays128-256) to make light colors.
128 *
129 * @paramthis is the Javadoc tag for documenting the purpose of parameters bgColor the color with which a contrast is desired; ifif executes the next statement only if the condition in parenthesis evaluates to true it
130 * is dark, go light and ifif executes the next statement only if the condition in parenthesis evaluates to true it is light, go dark.
131 *
132 * @returnnull a random color contrasting with the background color.
133 */
134 privateprivate is used to restrict access to the current class only Color getRandomContrastingColor(Color bgColor) {open braces start code blocks and must be matched with a close brace
135 intint is the type for whole numbers and it is short for integer low =this assignment operator makes the left side equal to the right side 128;// assume we want a light color
136 intint is the type for whole numbers and it is short for integer high =this assignment operator makes the left side equal to the right side 256;
137 intint is the type for whole numbers and it is short for integer averageBrightness =this assignment operator makes the left side equal to the right side (bgColor.getRed() +adds two numbers together or concatenates Strings together bgColor.getGreen() +adds two numbers together or concatenates Strings together
138 bgColor.getBlue()) / 3;
139
140 ifif executes the next statement only if the condition in parenthesis evaluates to true (averageBrightness >=this evaluates to true if the left side is not less than the right side 128) {open braces start code blocks and must be matched with a close brace// bgColor is light
141 low =this assignment operator makes the left side equal to the right side 0;// need dark color
142 high =this assignment operator makes the left side equal to the right side 128;// range
143 }close braces end code blocks and must match an earlier open brace
144
145 Game curr =this assignment operator makes the left side equal to the right side Game.getCurrentGame();
146 Color randomColor =this assignment operator makes the left side equal to the right side Game.getColor(curr.randomInt(low, high),
147 curr.randomInt(low, high), curr.randomInt(low, high));
148 returnreturn means to provide the result of the method and/or cease execution of the method immediately randomColor;
149 }close braces end code blocks and must match an earlier open brace
150 }close braces end code blocks and must match an earlier open brace
151
152 //Uploaded on Mon Mar 29 21:40:41 EDT 2010
|