The idea behind this, is that you have an array of PVector objects and at the beginning, you keep filling the array until the length becomes 20 and then you continue adding one element at a time and you remove the first element so the length stays at 20.
ArrayList<PVector> list_positions;
int length_limit = 20;
void setup() {
size(400, 400);
stroke(0);
strokeWeight(4);
list_positions = new ArrayList<PVector>();
}
void draw() {
background(255);
//Adding a new position
list_positions.add(new PVector(mouseX, mouseY));
if (list_positions.size() > length_limit) {
//Removing the first element
list_positions.remove(0);
}
//Drawing a line
beginShape();
for (PVector p : list_positions) {
vertex(p.x, p.y);
}
endShape();
}
A PVector is just a convenient way to store a group of two or three floats in a single variable. Plus it has some other handy functions related to vector math.