I have created an ArrayList of arrays. Each array in the ArrayList should contain the mouseX and mouseY positions for one drawing frame. The ArrayList is to be expanded by one element(array) each frame.
However, when accessing the ArrayList with a for-loop, all the elements in the list have the same value. What am I missing?
int[] pos = new int[2]; // mouseX and mouseY position
ArrayList<int[]> polygon = new ArrayList<int[]>(); // List containing many points with mouseX/mouseY positions
void setup() {
size(500, 500, P2D);
background(255);
strokeWeight(1);
stroke(1);
fill(255,0,0);
}
void draw() {
if (mousePressed) {
pos[0] = mouseX;
pos[1] = mouseY;
// Add current mouseX/mouseY-Array to Array-List
polygon.add(pos);
// Show contents of shape
for(int i = 0; i < polygon.size(); i++) {
println("POLYGON "+ i + " : " + polygon.get(i)[0], polygon.get(i)[1]);
}
}
}
void mouseReleased(){
polygon.clear();
}
pos[0] = mouseX;
pos[1] = mouseY;
// Add current mouseX/mouseY-Array to Array-List
polygon.add(pos);
But doesn’t the above code replace the array’s content so that the same array but with different numbers inside (the mouse position in each frame) is added every frame?