Why is my button placed like that?

I have:

void setup() {
  size(1250,800);
}

and I would like my button:

  rect(495,500,260,260,35); 

to be placed in the middle of the screen.

So I thought I could make a button

rect(button2X, buttonY, buttonSize, buttonSize, 35);

with:

int buttonSize = 260;
int buttonY = 500; 
int button2X = width/2-(buttonSize/2); 

but when I play the code, It looks like this:

PS. it’s the one on the far left, that is supposed to be in the middle like this:

1 Like

The problem is that the width variable is not set until the size() method is executed in setup()

Change your code to

int buttonSize = 260;
int buttonY = 500; 
int button2X; 

and in setup

void setup(){
    size(480, 320); // use your own width and height
    button2X = width/2-(buttonSize/2); 
    // rest of your setup code
}
2 Likes

That makes a lot of sense, thank you!!