Storing multiple values of the mouse position in a class

Hello! I am trying to program a rectangular “layout” creator that allows the user to draw multiple rectangles across the screen referencing the initial mouse pressed and the following mouse released coordinates as values for the rectangle width and height.

I managed to make it work with one rectangle however after using an array to start creating multiple ones I keep getting the same result where all the rectangles are on top of each other; none of them are keeping their own X and Y values and instead seem to be assigned to the same one.

In summary, I would like to draw a rectangle and have it keep its position instead of being reassigned each time the mouse is pressed. Please have a look at the code below and let me know what you think! (here the rectangle object is named window) Thank you in advance.

window[] wind = new window[10];

int total;

void setup() {

	size(1000, 1000);

	for (int i = 0; i < wind.length; ++i) {
		wind[i] = new window();


void draw() {


	for (int i = 0; i < total; ++i) {


void mousePressed() {

	total += 1;

	for (int i = 0; i < total; ++i) {

	println("total: "+total);


void mouseReleased() {

	for (int i = 0; i < total; ++i) {


class window {

PVector a,aa;
int x,y;
int[] newx = new int[20];
int[] newy = new int[20];
int[] newi = new int[20];
int[] newj = new int[20];
float i,j;


	void display(int f){

		a = new PVector(x, y);
		aa = new PVector(int(mouseX/50)*50, int(mouseY/50)*50);

		if (mousePressed) {
			rect(a.x, a.y, aa.x, aa.y);

		rect(newx[f], newy[f], newi[f], newj[f]);

		println("newx[f]: "+newx[f]);


	void mousePressed(int f) {

		x = int(mouseX/50)*50;
		y = int(mouseY/50)*50;
		newx[f] = x;
		newy[f] = y;
		// i = 0;
		// j = 0;
		println("x: "+x);
		println("y: "+y);


	void mouseReleased(int f) {

		i = aa.x;
		j = aa.y;
		newi[f] = int(i);
		newj[f] = int(j);


1 Like

That should be the core idea of your display function in the class

Don’t check the mouse in your display function or set variables there

You only want to display the old stored rectangles there


Instead don’t for loop in mousePressed or mouseReleased

Hey There!

Would you be looking for something like this?

PVector window[];
final int AR_SIZE = 100;
int i;
void setup() {
  size(640, 480);
  window = new PVector[AR_SIZE];
  for (int i = 0; i < AR_SIZE; i++) window[i] = new PVector(0, 0);
  i = 0;
void draw() {
  for (int i = 0; i < AR_SIZE; i++) showRect(window[i]);
  window[i] = new PVector(mouseX, mouseY);
  i = (i+1)%AR_SIZE;
void showRect(PVector pos) {
  fill(255, 0, 0);
  rect(pos.x, pos.y, 20, 20);

Thanks for the reply InferNova! But no not exactly what I need. I want to be able to draw one rectangle at a time as per the user’s mouse clicks. The code you shared creates more of a brush/line of rectangles that are limited to a certain number as the mouse position changes.

Sorry Chrisir, I am not sure I understand can you please clarify? The line of code you are referring to is just a marker tool to allow the user to see where they are clicking and creating their rectangle; that part works fine in my opinion.

Sorry for being a bit unclear.

The general approach of your sketch is a bit wrong.

What I was trying to say is that you are overthinking things a bit.

I just re-wrote your sketch and my version is more lean.

Try to make a version that stores all rects and add the idea later that the current rect we are drawing is also shown during the drawing process.

Your display() function in the class

Your display() function in the class should be much more lean. It should only display the rect (of the class) that’s already stored. You don’t check the mouse here because the rect is already closed.

Your mousePressed (outside the class)

And your mousePressed (outside the class) and mouseReleased (outside the class) should not loop over all the rects you already have but add ONE rect to your list.

Thats my mousePressed function (outside the class) :

void mousePressed() {
  // starting one new rect 
  total += 1;

Please work from here and show your entire code before we go on.