Weird rectangle

run the code and see, IDK why but the rectangle position is so weird, I tried to make a button and make it in the middle but for some reason, it won’t work

int rectSizeX = 300,rectSizeY = 250;     //  X and Y size of rect
int rectX = width/2;
int rectY = height/2;     // Position of square button   
color rectHighlight;
color currentColor, rectColor ,baseColor;
boolean rectOver = false;

void setup()
{
fullScreen();
rectMode(CENTER);
buttonSetup();
}
void draw()
{
  background(0);
  fill(0);
  MainButtom();
  update();
  if (resetPressed()){ rect(400,400,400,400);}
}

void buttonSetup(){
  rectColor = color(200);
  rectHighlight = color(130);
  baseColor = color(102);
  currentColor = baseColor;
}



void MainButtom() {
  if (rectOver) {
    fill(rectHighlight);
  } else {
    fill(rectColor);
  }
  stroke(102);
  rect(rectX, rectY, rectSizeX, rectSizeY);
}
void update() {
if ( overRect(rectX, rectY, rectSizeX, rectSizeY) ) {
    rectOver = true;
  } else {
rectOver = false;
  }
}
boolean overRect(int x, int y, int x2, int y2)  {
  if (mouseX >= x-x2/2 && mouseX <= x+x2/2 && 
      mouseY >= y-y2/2 && mouseY <= y+y2/2) {
    return true;
  } else {
    return false;
  }
}
boolean resetPressed() {
if (mousePressed && rectOver == true) {
  return true;
} else {
  return false;
  }
}

Hello,

we can’t run your code because you didn’t post your variables. Please post.

  • Also, did you give rectX, rectY values? Please note that before setup() width and height are 0 because they get their values only after size / fullScreen()!

  • Also, you use rectMode(CENTER); so this brings the button pos to the center of it.

editing the code with the variables now

I did everything you said sure, and about the rect mode thing, I want to progrem to work on every screen Im using it on and rectMode(CENTER) is one of the ways to make sure that the program will look the same on every screen (this button is a part of a bigger program)

As I said:

int rectX = width/2;
int rectY = height/2;

won’t work before setup()

Solution

divide it:

before setup

int rectX;
int rectY;

in setup after size

rectX = width/2;
rectY = height/2;

thank you for the help!!!

1 Like

You are welcome !!!

Chrisir