<< 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.

Termination of the setup method

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 .

Constructor for the Car class

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.)

Listing 3. beginning of the car class.

class Car { color c;float xpos; float ypos;float xspeed; // The Constructor is defined with arguments.Car(color tempC, float tempXpos,float tempYpos, float tempXspeed) {c = tempC; xpos = tempXpos;ypos = tempYpos; xspeed = tempXspeed;}
Listing 3. Beginning of the Car class.

Instance variables

The car class begins by declaring four instance variables to hold values for:

  • A color
  • An x-coordinate value
  • A y-coordinate value
  • A speed value

The constructor parameters

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 :

  • A color = red
  • An x-coordinate value = 0
  • A y-coordinate value = 100
  • A speed value = 2

Similarly, the constructor for the second instantiated object of the Car class received the following values:

  • A color = blue
  • An x-coordinate value = 0
  • A y-coordinate value = 10
  • A speed value = 1

The constructor body

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 .

Interpretation of the constructor parameters

Image 3 shows a screen shot of the display window while the sketch is running.

Image 3. screen shot of the sketch during execution.

Screen shot of the sketch during execution.
Image 3. Screen shot of the sketch during execution.

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.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, The processing programming environment. OpenStax CNX. Feb 26, 2013 Download for free at http://cnx.org/content/col11492/1.5
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'The processing programming environment' conversation and receive update notifications?

Ask