ArrayList of Arrays for Interactive Shapes

Hello,

I added some points and a print to explore your code.
You can draw to the screen while mouse is pressed.

Your code with my few modifications:

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(0);
  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);
    println(polygon.size());
 
    // Show contents of shape
    for(int i = 0; i < polygon.size(); i++) 
      {
      //println("POLYGON "+ i + "   :   " + polygon.get(i)[0], polygon.get(i)[1]);
      }
  
// GLV added this to plot points:
    for(int i=0; i < polygon.size(); i++)
      {
      stroke(255, 255, 0);
      strokeWeight(3);
      point(polygon.get(i)[0], polygon.get(i)[1]);
      }
    }
  }

void mouseReleased()
  {
  polygon.clear();
  background(0);
  }

:)

1 Like