# Weird rectangle

**URL:** https://discourse.processing.org/t/weird-rectangle/18073
**Category:** Coding Questions
**Created:** [February 24, 2020, 2:35pm UTC](https://discourse.processing.org/t/weird-rectangle/18073 "2020-02-24T14:35:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![bugotc14](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@bugotc14](https://discourse.processing.org/u/bugotc14)
#### Post date: [February 24, 2020, 2:35pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/1 "2020-02-24T14:35:04Z")

</div>

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

```auto
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;
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 24, 2020, 8:40pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/2 "2020-02-24T20:40:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bugotc14](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@bugotc14](https://discourse.processing.org/u/bugotc14)
#### Post date: [February 25, 2020, 8:23pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/3 "2020-02-25T20:23:45Z")

</div>

editing the code with the variables now

---

<div class="post-metadata">

### Author: ![bugotc14](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@bugotc14](https://discourse.processing.org/u/bugotc14)
#### Post date: [February 25, 2020, 8:33pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/4 "2020-02-25T20:33:44Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 25, 2020, 8:36pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/5 "2020-02-25T20:36:20Z")

</div>

As I said:

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

```

won’t work before setup()

**Solution**

divide it:

before setup

```auto
int rectX;
int rectY;

```

in setup after size

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

```

---

<div class="post-metadata">

### Author: ![bugotc14](https://avatars.discourse-cdn.com/v4/letter/b/35a633/32.png) [@bugotc14](https://discourse.processing.org/u/bugotc14)
#### Post date: [February 25, 2020, 8:39pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/6 "2020-02-25T20:39:14Z")

</div>

thank you for the help!!!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [February 25, 2020, 8:40pm UTC](https://discourse.processing.org/t/weird-rectangle/18073/7 "2020-02-25T20:40:01Z")

</div>

You are welcome !!!

Chrisir
