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 fang2.core.Game;
04 importimport means to make the classes and/or packages available in this program fang2.sprites.RectangleSprite;
05
06 importimport means to make the classes and/or packages available in this program java.awt.Color;
07 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
08
09 /**
10 * Place random boxes on screen and move them up (and loop to bottom).
11 * Mark the one closest to the top in red.
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 BoxParadeWithRed
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 /** index of highest box (in boxes) */
16 intint is the type for whole numbers and it is short for integer highest;
17
18 /** original color of the highest box in boxes */
19 Color highestColor;
20
21 /** The collection of RectangleSprites */
22 ArrayList<RectangleSprite> boxes;
23
24 /**
25 * 10 randomly colored and placed rectangles on the screen
26 */
27 @Override
28 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
29 // Make sure you initialize the collection!
30 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>();
31
32 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
33 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);
34 curr.setLocation(randomDouble(), randomDouble());
35 curr.setColor(randomColor());
36 addSprite(curr);
37 boxes.add(curr);
38 }close braces end code blocks and must match an earlier open brace
39
40 highest =this assignment operator makes the left side equal to the right side indexOfHighestBox();
41 highestColor =this assignment operator makes the left side equal to the right side boxes.get(highest).getColor();
42 boxes.get(highest).setColor(getColor("red"));
43 }close braces end code blocks and must match an earlier open brace
44
45 /**
46 * Find index of "highest" box on the screen. The lower the
47 * y-coordinate, the higher the box.
48 *
49 * @returnnull an index into boxes, the index of the highest box
50 */
51 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 indexOfHighestBox() {open braces start code blocks and must be matched with a close brace
52 intint is the type for whole numbers and it is short for integer indexOfHighestSoFar =this assignment operator makes the left side equal to the right side 0;
53
54 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 nextIndexToCheck =this assignment operator makes the left side equal to the right side 0; nextIndexToCheck !=this is the not equals operator which evaluates to true if both sides are different boxes.size();
55 ++this is the increment operator, which increases the variable by 1nextIndexToCheck) {open braces start code blocks and must be matched with a close brace
56 ifif executes the next statement only if the condition in parenthesis evaluates to true (boxes.get(nextIndexToCheck).getY() <
57 boxes.get(indexOfHighestSoFar).getY()) {open braces start code blocks and must be matched with a close brace
58 indexOfHighestSoFar =this assignment operator makes the left side equal to the right side nextIndexToCheck;
59 }close braces end code blocks and must match an earlier open brace
60 }close braces end code blocks and must match an earlier open brace
61
62 returnreturn means to provide the result of the method and/or cease execution of the method immediately indexOfHighestSoFar;
63 }close braces end code blocks and must match an earlier open brace
64
65 /**
66 * Move all the rectangles upward at a fixed speed.
67 */
68 @Override
69 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
70 intint is the type for whole numbers and it is short for integer currHighest =this assignment operator makes the left side equal to the right side indexOfHighestBox();
71
72 ifif executes the next statement only if the condition in parenthesis evaluates to true (currHighest !=this is the not equals operator which evaluates to true if both sides are different highest) {open braces start code blocks and must be matched with a close brace
73 boxes.get(highest).setColor(highestColor);
74 highest =this assignment operator makes the left side equal to the right side currHighest;
75 highestColor =this assignment operator makes the left side equal to the right side boxes.get(highest).getColor();
76 boxes.get(highest).setColor(getColor("SCG Red"));
77 }close braces end code blocks and must match an earlier open brace
78
79 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
80 boxes.get(i).translateY(-0.5 * dT);// move up
81
82 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
83 boxes.get(i).setY(1.0);
84 }close braces end code blocks and must match an earlier open brace
85 }close braces end code blocks and must match an earlier open brace
86 }close braces end code blocks and must match an earlier open brace
87 }close braces end code blocks and must match an earlier open brace
88
89 //Uploaded on Mon Mar 29 21:41:36 EDT 2010
|
Download/View scg/ch08/BoxParadeWithRed.java