This assignment was a bit harder, and I definitely wanted to do more than I ended up accomplishing. However, I did get the mouse clicks to store after clicking and hitting a key, and then after that a different code would load it in and then show where you clicked. I was hoping to get it to a point where it would wait after each square and show in order where you would click.

I also considered trying to get it into one file instead of two, but wasn’t able to do that either!

I was overall happy with how it turned out but frustrated that I was unable to do more.

int xCoord=400; 
int yCoord=400;
int numCoord = 0;
PrintWriter mouseInput;

void setup() {
  size(800, 800);
  mouseInput = createWriter("coordinates.txt");
}


void draw() {
  background(#15C2D3);

  textSize(20);
  text("Click around, then hit any key", 385, 370);

  square(xCoord-25, yCoord-25, 50);

  if (mousePressed && mouseX > 25 && mouseX < 775 && mouseY > 25 && mouseY < 775) {

    xCoord= mouseX;
    yCoord = mouseY;
    
    mouseInput.println(mouseX);
    mouseInput.println(mouseY);
    numCoord ++;
    delay(300);
  }
}

void keyPressed(){
  mouseInput.flush(); //flushes printWriter object, writes the data before it closes
  mouseInput.close();
  exit();
}
String [] coords; // square brackets indicates it's an array. multiple values in one
void setup() {
  size(800, 800);
  coords = loadStrings("coordinates.txt");
  
  background(#15C2D3);
}

void draw(){
  textSize(15);
  text("This is where you clicked :)", 240, 240);
  
  for (int x = 0; x < coords.length; x += 2) {
    int y = x + 1;
    square(float(coords[x])-25, float(coords[y])-25, 50);
  }
}
Posted in

Leave a comment