From FANG
|
01 packagepackage is used to name the directory or folder a class is in scg.ch08;
02
03 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
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.RectangleSprite;
07
08 /**
09 * Randomly place some number of rectangle sprites. Then move them
10 * upward at a fixed rate, looping them around off the top of the screen
11 * to the bottom.
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 BoxParade
14 extendsextends means to customize or extend the functionality of a class Game {open braces start code blocks and must be matched with a close brace
15 /** The collection of RectangleSprites */
16 ArrayList<RectangleSprite> boxes;
17
18 /**
19 * 10 randomly colored and placed rectangles on the screen
20 */
21 @Override
22 publicpublic is used to indicate unrestricted access (any other class can have access) voidvoid means the method does not return a value setup() {open braces start code blocks and must be matched with a close brace
23 // Make sure you initialize the collection!
24 boxes =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<RectangleSprite>();
25
26 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 10; ++this is the increment operator, which increases the variable by 1i) {open braces start code blocks and must be matched with a close brace
27 RectangleSprite curr =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor RectangleSprite(0.1, 0.1);
28 curr.setLocation(randomDouble(), randomDouble());
29 curr.setColor(randomColor());
30 addSprite(curr);
31 boxes.add(curr);
32 }close braces end code blocks and must match an earlier open brace
33 }close braces end code blocks and must match an earlier open brace
34
35 /**
36 * Move all the rectangles upward at a fixed speed.
37 */
38 @Override
39 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
40 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 boxes.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
41 boxes.get(i).translateY(-0.5 * dT);// move up
42 ifif executes the next statement only if the condition in parenthesis evaluates to true (boxes.get(i).getY() < 0.0) {open braces start code blocks and must be matched with a close brace// loop around at top
43 boxes.get(i).setY(1.0);
44 }close braces end code blocks and must match an earlier open brace
45 }close braces end code blocks and must match an earlier open brace
46 }close braces end code blocks and must match an earlier open brace
47 }close braces end code blocks and must match an earlier open brace
48
49 //Uploaded on Mon Mar 29 21:41:10 EDT 2010
|
Download/View scg/ch08/BoxParade.java