<< Chapter < Page Chapter >> Page >

Recall also that if you plan to export your sketch in JavaScript mode or to export your sketch as a stand-alone application, you must use literal values asthe parameters to the size method.

You should set the height of the display window to be at least 70 pixels greater than the height of the image to allow roomto display the width, height, and pixel information below the image as shown in Image 4 . Otherwise, the black text will be printed on the image and may not be visible, depending on the color of theimage.

If you don't know the size of your image, simply make a guess. You will either get an output that displays the size of the image, as in Image 2 and Image 3 , or you will get an output that shows the image as in Image 1 and Image 4 . In either case, the width and height of the image will be displayed and you can use this information to update the call tothe size method and recompile the sketch.

Set the font

The only other thing that is new in Listing 2 is the call to the createFont method.

This statement is required to prepare the sketch to draw the text characters on the display window as shown in Image 1 . I will leave it as an exercise for the student to study the documentation for the createFont method in whatever depth is necessary to understand it.

The draw method

The draw method is shown in its entirety in Listing 3 . As you can see, all the duties of the draw method were delegated to the run method belonging to an object of the class named Pr0120aRunner .

Listing 3. the draw method.

void draw(){ obj.run();}//end draw
Listing 3. The draw method.

You might be wondering why I didn't simply write that code in the draw method. The reason is that I frequently provide my students with input and output images (such as Image 1 and Image 4 ) along with the driver code in Listing 10 . It is then the students responsibility to design and write the code in Listing 11 .

In other words, I provide a driver class along with the input and output images. It is the student's responsibility todesign and write a class that will run in conjunction with the driver class to transform the input image into the output image.

The class named Pr0120aRunner

Beginning of the class

The class named Pr0120aRunner begins in Listing 4 .

Listing 4 also shows the beginning of the method named run that is called by the draw method in Listing 3 .

Listing 4. beginning of the class named pr0120arunner.

class Pr0120aRunner{void run(){ background(255);//whitetextFont(font,16);//Set the font size, and colorfill(0);//black textloadPixels();//required img.loadPixels();//requiredfloat reD,greeN,bluE;//store color values hereint ctr = 0;//output pixel array counter
Listing 4. Beginning of the class named Pr0120aRunner.

The run method begins by setting the background of the display window to white, and then takes care of some more housekeeping details regarding fonts.

Daniel Shiffman tells us about the requirement to use the loadPixels method and the updatePixels method in his tutorial titled Images and Pixels . I will leave it as an exercise for the student to study and understand that material.

Finally, Listing 4 declares some local variables that will be used later.

Check for size problems

The code in Listing 5 checks to confirm that the image will fit in the display window. If not, one of the messages shown in Image 2 and Image 3 is displayed in place of the image.

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