04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

434<br />

In addition to loadImage(), you can use the image() function to actually draw the image<br />

to the screen, as in the line image(img1, 0, 0);. The second <strong>and</strong> third arguments in the<br />

call (0, 0) are for the x <strong>and</strong> y position of the image. Images are positioned from the topleft<br />

corner of the image. In the last example, if I had wanted to center the image in the display<br />

window (assuming, of course, that the image was smaller than the window), I could<br />

have used the following:<br />

//load <strong>and</strong> center an image<br />

size(700, 500);<br />

PImage img1;<br />

img1 = loadImage("heidi.jpg");<br />

translate(width/2, height/2);<br />

image(img1, -img1.width/2, -img1.height/2);<br />

Since PImage is a data type (think class), it includes properties (also referred to as fields)<br />

<strong>and</strong> methods. PImage objects have width <strong>and</strong> height properties, which I conveniently used<br />

in calculating the image position (centered). More information about the PImage data type,<br />

including its other fields/methods, can be found at http://processing.org/reference/<br />

PImage.html.<br />

In the last example, I called the image() function to display my image. set() can also be<br />

called to load images—in this case, two copies of the same image—the same function<br />

called earlier to change the color of a pixel (see Figure 10-24):<br />

//Loading Images with set()<br />

size(600, 400);<br />

background(245);<br />

PImage img2 = loadImage("kids.jpg");<br />

set(50, 50, img2);<br />

set(img2.width+100, 50, img2);<br />

Figure 10-24. Loading Images with set() sketch

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!