Looping through a List in Processing

Here is a piece of demonstration code to use various ways to loop through a List or ArrayList in Processing, i.e. Java. The first two examples use for loop and the third one uses the while loop.
 
We initialize the ArrayList with random integers.

ArrayList<Integer> iList = new ArrayList<Integer>();
iList.add(floor(random(10)));
iList.add(floor(random(10)));
iList.add(floor(random(10)));

The first method is the traditional way to loop with an index.

for (int i=0; i<iList.size(); i++) {
    println(iList.get(i));
}

The second method also uses the for loop, but with alternate syntax.

for (int i : iList) {
    println(i);
}

The third method uses the Iterator through the ArrayList.

Iterator<Integer> it = iList.iterator();
while (it.hasNext()) {
    int i = it.next();
    println(i);
}

Processing Test with the PGraphics

To simplify the use of a dynamic mask with image, I try to use the PGraphics class as an off screen buffer to store the image for a subsequent mask operation. The foreground image is the live video input from the webcam. The mouse drag operation will draw a dynamic mask to reveal the webcam image. It makes use of the fact that the PGraphics class is a subclass of PImage. The mask function can directly take the PGraphics instance as input. Here is a sample screen shot.
 

Continue reading

Processing Performance Test 1

I try to compare various methods to handle mainly image-based computer graphics in the Processing environment and publish the results for developers’ reference. The first one is a very straightforward test by comparing two ways to modify all pixels in a single PImage object instance.

The first way is nested loops for x and y dimensions and the second way is to traverse the whole pixels array in one linear loop.
 
Continue reading