<< Chapter < Page Chapter >> Page >

Given that the 0,0 origin is at the upper left, you should be able to correlate the vertical position ( y-coordinate value ) of each rectangle with the color of the rectangle.

Because Image 3 is a screen shot, there is nothing in Image 3 that corresponds to the speed parameter in Listing 3 . You will see later that this parameter is used to establish the speed with which each rectangle moves across the displaywindow. Stated differently, each rectangle appears to move across the display window in incremental steps. The speed value is used by the drive method to compute the size of each incremental step.

The draw method of the Cars class

Returning now to the Cars class, the code fragment in Listing 4 shows the draw method in its entirety.

Listing 4. the draw method of the cars class.

void draw() { background(255);myCar1.drive();myCar1.display();myCar2.drive(); myCar2.display();}
Listing 4. The draw method of the Cars class.

Recall that by default, the draw method is called repeatedly 60 times per second. Nothing was done in this sketch to changethat.

Set the background color

The draw method begins by calling the inherited background method to erase everything on the display window and set its color to white.

There are seven overloaded versions of the background method where different parameter types and order are used to produce different behavior.

Here is part of what the documentation has to say about the background method.

"The background() function sets the color used for the background of the Processing window. The default background is light gray. background() is typically used within the draw() function to clear the display window at the beginning of each frame.

An image can also be used as the background for a sketch, although the image's width and height must match that of the sketch window. To resize an image to the size of the sketch window, use image.resize(width, height).

Images used with background() will ignore the current tint() setting.

It is not possible to use transparency (alpha) with background colors on the main drawing surface; you can achieve the same effect with createGraphics()."

The version of the background method that is called in Listing 4 takes a single parameter. That versions sets the background color to a gray value between white and black. A parameter valueof 255 produces white and a parameter value of 0 produces black.

Call methods on the Car objects

If you are new to OOP, you may be unfamiliar with the following syntax:

myCar1.drive();

Basically this means:

  • Go find the object whose reference is stored in the reference variable named myCar1 .
  • Knock on the object's door and ask it to execute its method named drive .

Each time the draw method is executed, it asks each Car object to first execute its drive method and then execute its display method.

At this point, we don't know what that means exactly because we haven't examined the behavior of the methods named drive and display belonging to the Car objects.

The display method of the Car class

Switching again to the Car class, the code fragment in Listing 5 shows the display method of the Car class.

Listing 5. the display method of the car class

void display() { stroke(0);fill(c); rectMode(CENTER);rect(xpos,ypos,20,10); }//end method named display
Listing 5. The display method of the Car class

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