Recently I've been looking at Povray, pyprocessing, and cfdg (version 3.0) as tools for creating digital images. I have branched two separate blogs where I mainly explore jruby + processing and

Tuesday, 26 May 2009

Collections in Processing

Welcome to our old friend ArrayList, which can be used in processing, its back to the old days of storing objects, and recasting them back to what you knew you had stored in the first no parameterized lists.......
Also don't try getting to clever you can't store the ArrayList as a generic List.....
Well that's not exactly true (however you need to import java.util.List, because List could be ambiguous).

   1:import processing.core.*;
2:// import java.util.List; if you want use a generic List uncomment this line
4:ArrayList vertices; // declare an expandable collection class
6:void setup() {
7: size(400, 400);
8: vertices = new ArrayList(); // initialise the collection class
9: background(255);
11:// record vertex when mouse clicked (you can keep going even after drawing has been rendered)
12:void mousePressed() {
13: vertices.add(new PVector(mouseX, mouseY)); // adds an anonymous PVector instance
16:// Pressing any key draws the currently stored vertices as a line strip
17:void keyPressed() {
18: float x = 0.0f;
19: float y = 0.0f;
20: PVector vert; // a temporary PVector instance to hold an instance of PVector read from the collection
21: stroke(255, 0, 0);
22: strokeWeight(5);
23: noFill();
24: beginShape(); // begin the recording of vertices
25: for (int i = 0; i < vertices.size(); i++) { // iterating the verices ArrayList
26: vert = (PVector)vertices.get(i); // need to cast the stored object to PVector
27: x = vert.x;
28: y = vert.y;
29: vertex(x, y); // the vertex to be recorded
30: }
31: endShape();
34:void draw(){ // probably required to keep program running

PS I can confirm that if you post pre-formatted text from jEdit to the blog there is no issue with carriage return/linefeed etc creating a new line, see Sharons blog if you are interested about the linefeed issue. I have also updated (my personal) jEdit processing mode to include ArrayList a late addition?

No comments:

Post a Comment


Blog Archive

About Me

My photo
Pembrokeshire, United Kingdom
I have developed JRubyArt and propane new versions of ruby-processing for JRuby- and processing-3.2.2