# Display size calculations problem

**URL:** <https://discourse.processing.org/t/display-size-calculations-problem/44151>\
**Category:** Coding Questions\
**Created:** [March 24, 2024, 3:28pm UTC](https://discourse.processing.org/t/display-size-calculations-problem/44151 "2024-03-24T15:28:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jellyfish\_Grave](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jellyfish_grave/32/19452_2.png) [@Jellyfish\_Grave](https://discourse.processing.org/u/Jellyfish_Grave)\
**Post date:** [March 24, 2024, 3:28pm UTC](https://discourse.processing.org/t/display-size-calculations-problem/44151/1 "2024-03-24T15:28:39Z")

</div>

I came accross a problem while making my current project. The concept would be to create a black square on a white background of 1000 by 1000 pixels, but have it be at the center of the screen. Taking into account the fact that this should work on different sizes of screens, I tried this:

```auto
int fartop=displayHeight-1000/2;
int farleft=displayWidth-1000/2;

void setup(){
  size(1000,1000); //I think this doesn't matter since I put the project on full screen one line later
  fullScreen();
  background(255);
}

void draw(){
  fill(0);
  rect(farleft, fartop, 1000, 1000);
}

```

However, the result I got was this:

 ![display problem screenshot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/c/9/c9abe83a21d1a5356452aae1c7d6933608955056.png)  
My screen size is 1920 by 1080, so logically, farleft should be equal 460 and fartop should be equal 40. But after checking the variables’ values directly, I realised they were both equal -500.  
I then tried defining the variables as such to understand the problem:

```auto
int fartop=displayHeight-1000;
int farleft=displayWidth-1000;

```

And after this, both variables were equal -1000.  
When defining the variables as just my display sizes, it gave the correct screen resolution I’m using, but as soon as I start affecting the variable, it ignores it for some reason.  
I’ve also tried the following:

```auto
int fartop=displayHeight;
int farleft=displayWidth;

void setup(){
  size(1000,1000);
  fullScreen();
  background(255);
  fartop=fartop-1000/2;
  farleft=farleft-1000/2;
}

```

But the result is the same. I have no idea what could be causing this, and I’d appreciate some insight grealy. Thank you in advance.

---

<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:** [March 24, 2024, 3:47pm UTC](https://discourse.processing.org/t/display-size-calculations-problem/44151/2 "2024-03-24T15:47:14Z")

</div>

Three points:

- The position 0,0 is in the upper left corner and not in the center

- Maybe `displayWidth` and `displayHeight` get defined only after `size()` or `fullscreen()`? So is `width` and `height` by the way.

- I wouldn’t use `size` and `fullscreen` together. Just use the latter (one or the other).

---

<div class="post-metadata">

**Author:** ![Jellyfish\_Grave](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jellyfish_grave/32/19452_2.png) [@Jellyfish\_Grave](https://discourse.processing.org/u/Jellyfish_Grave)\
**Post date:** [March 24, 2024, 4:00pm UTC](https://discourse.processing.org/t/display-size-calculations-problem/44151/3 "2024-03-24T16:00:52Z")

</div>

Thank you for the answer!

Thank you for the reminder! That is why I substract the size of the square then divide the remaining space by two, in order to get the same space on both sides!  
After modifying my code as such:

```auto
int fartop=0;
int farleft=0;

void setup(){
  fullScreen();
  background(255);
  fartop=(displayHeight-1000)/2;
  farleft=(displayWidth-1000)/2;
}

void draw(){
  fill(0);
  rect(farleft, fartop, 1000, 1000);
}

```

It gave me the awaited result! Much thanks for the help!

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [March 24, 2024, 4:29pm UTC](https://discourse.processing.org/t/display-size-calculations-problem/44151/4 "2024-03-24T16:29:05Z")

</div>

Hello @Jellyfish_Grave,

Another way:

```auto
void setup()
  {
  println(displayWidth, displayHeight); //print before fullscreen()
  fullScreen();
  background(255);
  println(displayWidth, displayHeight); //print after fullscreen()
  }

void draw()
  {
  fill(0);
  translate(displayWidth/2, displayHeight/2);
  rectMode(CENTER);
  rect(0, 0, 1000, 1000);
  }

```

I encourage you to peruse the tutorials, references and examples here:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

`:)`

---

<div class="post-metadata">

**Author:** ![Jellyfish\_Grave](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jellyfish_grave/32/19452_2.png) [@Jellyfish\_Grave](https://discourse.processing.org/u/Jellyfish_Grave)\
**Post date:** [March 24, 2024, 4:46pm UTC](https://discourse.processing.org/t/display-size-calculations-problem/44151/5 "2024-03-24T16:46:42Z")

</div>

I see, thank you! This does clear some things up, I appreciate the help given!  
I will check this link out, thank you for the ressource.
