matthewalangreen
10/31/2018 - 6:03 PM

PVector

PVector ArrayList

int xPos;
PVector position;
ArrayList<PVector> positions = new ArrayList<PVector>();

void setup() {
  size(400,400);
  background(255);
  rectMode(CENTER);
  noStroke();
  xPos = width/2 - 20;
  position = new PVector(width/2 - 20, height - 20);
 
}

void draw() {
  background(255);
  fill(255,0,0);
 // rect(width/2, height/2, 40, 40);
 
 // this "moves" because two parameters are changing:
 // mouseX and mouseY
 // rect(mouseX,mouseY, 40, 40);
  
  
  // i want to draw 10 rectangles,
  // each with two positions that
  // switch when i press a button
  
  
  if (keyPressed) {
   if(key == 's') {
     xPos = xPos + 20;
     if(xPos > width) { // reset
       xPos = width/2 - 20;
     }
   }
  }
  
  //rect(xPos, 50, 20, 20);
  //rect(xPos, 100, 20, 20);
  //rect(xPos, 150, 20, 20);
  //rect(xPos, 200, 20, 20);
  //rect(xPos, 250, 20, 20);
  
  fill(0,0,255);
  rect(position.x, position.y, 30, 30);
  
  // create PVector positions for my rectangles
  // add them to my positions ArrayList
  for(int i = 0; i<5; i++) { // do this stuff 5 times
   positions.add(new PVector(width/2 - 20,50 + i*50));
  }
  
  // draw the rectangles for each position in my 
  // positions ArrayList
  for(int i = 0; i<5; i++) {
    rect(positions.get(i).x, positions.get(i).y, 20, 20);
  }
  
  // change the value of positions[4]
  PVector oldVector = positions.get(4);
  PVector newVector = new PVector(width/2 + 50, oldVector.y);
  positions.set(4,newVector);
  
  
  // fill red so we can see it
  fill(255,0,0);
  rect(positions.get(4).x, positions.get(4).y, 20, 20);
  
}