Why is my button placed like that?

I have:

void setup() {

and I would like my button:


to be placed in the middle of the screen.

So I thought I could make a button

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


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

That makes a lot of sense, thank you!!