<< Chapter < Page | Chapter >> Page > |
Note that if only one value is provided to color(), it will be interpreted as a grayscale value. Add a second value, and it will be used for alpha transparency. When three values are specified, they are interpreted as either RGB or HSB values. Adding a fourth value applies alpha transparency."
Therefore, the calls to the color method in the two calls to the Car constructor in Listing 2 create and pass color values for pure red and pure blue as parameters to the constructor.
That completes the discussion of the setup method in Listing 2 . When the method terminates, the size of the display window has been set to 200 x 200 pixels. Two objects of the Car class have been instantiated and those object's references have been saved inthe instance variables named myCar1 and myCar2 .
Without having examined the constructor code for the Car class yet, about all we know about the Car objects is that a red color value was passed to the constructor for one object and a blue color value was passed to the constructor for the other object.
This looks like a good time to examine the constructor for the Car class in order to understand the meaning of the other constructor parameters in Listing 2 .
The code fragment in Listing 3 shows the beginning of the Car class down through the constructor. (The code fragment in Listing 3 was extracted from Listing 1 to make it easier to discuss.)
The car class begins by declaring four instance variables to hold values for:
Comparing the order of the constructor parameters in Listing 3 with the parameter values in Listing 2 tells us that the constructor for the first instantiated object of the Car class received the following values :
Similarly, the constructor for the second instantiated object of the Car class received the following values:
The four statements in the body of the constructor in Listing 3 simply save the values of the incoming parameters in the corresponding instance variables discussed above .
Image 3 shows a screen shot of the display window while the sketch is running.
Looking at Image 3 , you can probably guess that the color values passed to the constructor are used to establish the visible color of each of the two rectangles.
Recall that this sketch is an animation where the rectangles move across the screen from left to right at different speeds You will see later that thecoordinate values received by the constructor specify the initial positions of the rectangles and that the y-coordinate values never change.
Notification Switch
Would you like to follow the 'The processing programming environment' conversation and receive update notifications?