Processing is really just a collection of libraries that makes some Java tasks easer (we’ll talk more about libraries further down when we talk about sound). Such as:

A set of functions for drawing shapes.

A set of functions for loading and displaying text, images, and video.

A set of functions for mouse and keyboard interaction.

A simple development environment to write your code.

All semester long as we’ve been typing in Processing’s window, we’ve been using Processing’s IDE (Integrated Development Environment). We can write Processing (and plain old Java) in other environments, like Eclipse. You can use a tool called “Proclipse” to use Processing in Eclipse.

While the Processing reference has been our bible all semester, we are not limited to the functions listed there. Because we’re actually writing Java, we can use anything in the Java API (which as a warning- is HUGE and hard to look at. We generally use this just for looking things up).

One of these useful Java classes is the ArrayList.

Array Lists

When we were first learning about arrays, I made a big point of telling you that the size of an array couldn’t be changed. Then you found out I was lying when we looked at Processing’s append() function, for resizing arrays. But append only let us tack on a space at the end of the array. What we really want is a flexible sized array that we can add to and delete from on the fly (like when a ball bounces off screen), from the beginning, middle and end.

That’s what Java’s Array List does. Take a look at the Java API documentation to get a sense of what it does- http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Random.html

We’re going to create an ArrayList of a particle object. Here’s the particle class- it’s just a simple ellipse with an xpseed and a yspeed.

Now we can add particles to the particle container dynamically (aka, in the draw loop). We can only add particle objects to the container- we can’t mix and match data types.

void draw(){
particles.add(new Particle()); // this will add a particle to the container every time we cycle through the draw loop
}

When we want to call the functions associated with our object, we still need to iterate through the list with a for loop:

for (int i = 0; i < particles.size(); i++) { //note that we call size instead of length- this is dynamic and will update when a particle is added or subtracted
Particle p = (Particle) particles.get(i); //this is new!
p.run();
p.gravity();
p.display();
}

So this line is new– Particle p = (Particle) particles.get(i); //this is new!

Because ArrayLists are all loosey goosey and allow us to add to and take from them on the fly, the tradeoff is that we have to convert the object back to its original data type when we want to do something to it (like call a function or retreive its x position). The syntax above is called “casting”- it’s telling Processing what data type we’re dealing with. We’ve seen this before when we called int(float x); to return an int to us when rpevious we were dealing with a float.

We can also remove objects from our list very easily. This is a good idea for instances like when a ball travels offscreen and we no longer need it:

if (particles.size() > 100) {
￼ particles.remove(0);
}

Here, 0 pertains to the index of the object we want to take away. This is taking away the first object, but we can specify any index number we’d like.

Text that is treated as actual written word rather than code is called a String. Strings are technically a Java class, and not part of Processing specifically. We’ve seen strings before whenever we’ve printed information out to the console, or loaded in an image-

println("collision!");
myImage = loadImage("myImage.jpg");

Text is stored in the data type Strings. The concept is the same as storing ints, floats, booleans, except this stores an array of characters.

String sometext = "How do I make a String? Just type some letters between quotes!";

A string is technically an object with methods that you can call. Check out the Processing reference page for a full list of the functions you can call on a string.

One feature of String objects is concatenation, which is joining two Strings together. Strings are joined with the “ + ” operator.While this usually means add in the case of numbers, in Strings it means join.

String helloworld = "Hello" + "World";

Variables can also be brought into a String using concatenation. int x = 10;

String message = "The value of x is: " + x;

We saw a good example last week when we loaded in an array of images with numbered filenames.

Manipulating Strings is offers a world of possibilites, like using outside data (like a Twitter stream), and plenty of programmers specialize in doing just that. But for our purposes, it’s more useful to focus on how to display text (like instructions, scores, timers, etc.).

To display text, you need to load in a font. The process is actually similar to loading in an image. Go to Tools –> “Create Font”. This will create and place the font file in your data directory. Make note of the font filename for next step.

Declare an object of type PFont.

PFont f;

Just like with PImage, you need to load the font into “f’. Use the name of the file you just created in the first step.

f = loadFont("HelveticaNeue-Light-24.vlw");

Specify the font using textFont( ). textFont( ) takes one or two arguments, the font variable and the font size, which is optional.

textFont(f, 24);

fill() will also affect your font color, so add a fill(0); if you want black text, for instance.

Call the text( ) function to display text. text() is just like shape or image drawing, it takes three arguments—the text to be displayed, and the x and y coordinate to display that text.)

Archives

Categories

Meta

The OpenLab at City Tech:A place to learn, work, and share

The OpenLab is an open-source, digital platform designed to support teaching and learning at City Tech (New York City College of Technology), and to promote student and faculty engagement in the intellectual and social life of the college community.

Copyright

The OpenLab at City Tech:A place to learn, work, and share

The OpenLab is an open-source, digital platform designed to support teaching and learning at City Tech (New York City College of Technology), and to promote student and faculty engagement in the intellectual and social life of the college community.