We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This code is designed to store the x & y values of mouse clicks in 2 arrays. I have set it up with an initial 3 elements and want the arrays to expand should there be more mouse clicks. On the 3rd mouse click I get an out of bounds exception:3 message. The append() instruction does not appear to have expanded the arrays. What have I done wrong?
boolean loaded=false; int[] verticex = new int[3]; int[] verticey = new int[3]; int nev=3; //number of elements in vertices
void draw() { if (loaded==false){ loadPixels(); bg.loadPixels(); updatePixels(); background(bg); loaded=true; } }//draw
//append array with each mouse click void mouseClicked() { if (nev<4){ for (int cv=0;cv<nev;cv++){ verticex[cv]=mouseX; verticey[cv]=mouseY; } }else{ append(verticex, verticex[0]); append(verticey, verticey[0]); verticex[0]=mouseX; verticey[0]=mouseY; verticex[nev-2]=mouseX; verticey[nev-2]=mouseY; } nev=nev+1; printArray(verticex); printArray(verticey);
Answers
https://forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
General tip: if append() is needed, it means regular arrays aren't the right container!
For whole values, a dynamic container such as IntList is the best option:
https://Processing.org/reference/IntList.html
Thanks. Lists certainly look like a better option here than arrays so I have amended the code accordingly. It wants me to put curly brackets around the list declarations but if I do this or declare them in setup it comes back with the message 'variable doesn't exist'.
PImage bg;
verticesX = new IntList();
isn't a declaration but an assignment!final IntList verticesX = new IntList();
Note that the
final
keyword will prevent allocating another list to verticesX anywhere else in your program. If this causes a problem then simply use.IntList verticesX = new IntList();
In this case I decided I didn't need the final keyword