# Using surface.setLocation does not do same thing every time

**URL:** https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871
**Category:** Coding Questions
**Created:** [June 5, 2019, 7:01pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871 "2019-06-05T19:01:14Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![DerekCresswell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derekcresswell/32/4868_2.png) [@DerekCresswell](https://discourse.processing.org/u/DerekCresswell)
#### Post date: [June 5, 2019, 7:01pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/1 "2019-06-05T19:01:14Z")

</div>

I’m using surface.setLocation to position my windows. With the arguments passed into my two windows they should be at the same X and one height difference in the Y. The Y seems ok but the X is off by about 10 or so pixels.  
both have same height and width and are using (displayWidth / 2 - width) as X argument. whats wrong?  
Also just to add what is / how to get the height of the bar above a window? Like with the name and X button.

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [June 6, 2019, 1:22pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/2 "2019-06-06T13:22:46Z")

</div>

Hey There!

Background code, or a small example to showcase the problem would be great !

---

<div class="post-metadata">

### Author: ![DerekCresswell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derekcresswell/32/4868_2.png) [@DerekCresswell](https://discourse.processing.org/u/DerekCresswell)
#### Post date: [June 6, 2019, 5:46pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/3 "2019-06-06T17:46:37Z")

</div>

```auto

  void setup(){
    size(300, 200);
    PApplet.runSketch(new String[] {"window"}, new SecondWindow());
    surface.setResizable(true);
    surface.setSize(300, 200);
    surface.setLocation(displayWidth / 2 - width, displayHeight / 2 - height);
  }
  
  void draw(){
    background(100, 45, 45);
  }
  
  class SecondWindow extends PApplet{
    
    void settings(){
      size(300, 200);
    }
    
    boolean flag = true;
    void draw(){
      
      if(flag){
        flag = false;
        surface.setLocation(displayWidth / 2 - width, displayHeight / 2);
      }
      
      background(100, 45, 100);
    }
    
  }

```

Doesn’t do it every time so just run it a few times.

---

<div class="post-metadata">

### Author: ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)
#### Post date: [June 6, 2019, 10:43pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/4 "2019-06-06T22:43:50Z")

</div>

Hey There!

I am not expert on this myself here, but placing the setSize in the main draw() seems to fix the issue all then you need to is add it’s location. It seems very bizarre because if you do setLocation() in the main draw the window doesn’t grow in size ( it’s not the size specified ) so I would have like a variable to initialize a variable which sets the location of it after draw is executed. Here they are the same size in the sketch below ( I did strip a few things away, it’s seems the working ground here is thin ice. That it any minor changes influences the sketch windows in a bizarre and unexpected way.)

```auto
void setup() {
  PApplet.runSketch(new String[] {"window"}, new SecondWindow());
  size(300, 200);
}

void draw() {
  background(100, 45, 45);
  surface.setSize(300, 200);
}

class SecondWindow extends PApplet {

  void settings() {
    size(300, 200);
  }

  boolean flag = true;
  void draw() {
    background(100, 45, 100);
    surface.setSize(300, 200);
    surface.setLocation(displayWidth / 2 - width, displayHeight / 2);
  }
}

```

---

<div class="post-metadata">

### Author: ![DerekCresswell](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/derekcresswell/32/4868_2.png) [@DerekCresswell](https://discourse.processing.org/u/DerekCresswell)
#### Post date: [June 8, 2019, 4:22am UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/5 "2019-06-08T04:22:29Z")

</div>

Using set size every draw is not efficient. The reasons set location is in the draw function is because on the second Applet the window hasn’t been created and can’t be set in the settings.

---

<div class="post-metadata">

### Author: ![esc746](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/esc746/32/4258_2.png) [@esc746](https://discourse.processing.org/u/esc746)
#### Post date: [March 27, 2023, 1:08pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/6 "2023-03-27T13:08:12Z")

</div>

This is still a problem in 2023. The following code leaves a small border to the left of the window.

surface.setLocation(0, 0);

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 27, 2023, 4:43pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/7 "2023-03-27T16:43:43Z")

</div>

The following code worked for me using P4.2 on Mac osx. The two windows arae aligned vertically and no need to set the size in draw (which is not recommended)

```auto
void setup() {
  size(300, 200);
  surface.setResizable(true);
  surface.setLocation(displayWidth / 2 - width, displayHeight / 2 - height);
  PApplet.runSketch(new String[] {"window"}, new SecondWindow());
}

void draw() {
  background(100, 45, 45);
}

class SecondWindow extends PApplet {

  void settings() {
    size(300, 200);
  }

  void setup() {
    surface.setLocation(displayWidth / 2 - width, displayHeight / 2);
  }
  
  void draw() {
    background(100, 45, 100);
    
  }
}

```

---

<div class="post-metadata">

### Author: ![esc746](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/esc746/32/4258_2.png) [@esc746](https://discourse.processing.org/u/esc746)
#### Post date: [March 28, 2023, 2:01pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/8 "2023-03-28T14:01:04Z")

</div>

That code won’t reveal the bug, unless your eyes can tell the difference between a 10 pixel (approx) border to the left! Try locating the window at (0, 0).

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 28, 2023, 2:31pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/9 "2023-03-28T14:31:17Z")

</div>

Sorry about that I was answering the original question and did not read your post of a day ago regarding location [0,0]

I have modified my code to make use of location [0,0] as shown below and added an ellipse to help check the sketch borders. Here is a screen grab that shows the windows horizontally aligned and no visible 10px border. Perhaps you could provide code and screen grab to show what you mean.

 ![Screenshot 2023-03-28 at 15.27.47](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/a/6ab737d747a3e971d20dafff119aaa3d31c65acb.png)

If I use location [0,0] for both windows the first window is perfectly covered by the second.

```auto
void setup() {
  size(300, 200);
  surface.setResizable(true);
  surface.setLocation(0, 0);
  PApplet.runSketch(new String[] {"window"}, new SecondWindow());
}

void draw() {
  background(100, 45, 45);
  fill(0, 200, 200);
  ellipse(width/2, height/2, width, height);
}

class SecondWindow extends PApplet {

  void settings() {
    size(300, 200);
  }

  void setup() {
    surface.setLocation(0, 200);
  }

  void draw() {
    background(100, 45, 100);
    fill(200, 200, 0);
    ellipse(width/2, height/2, width, height);
  }
}

```

---

<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: [April 13, 2026, 6:39pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/10 "2026-04-13T18:39:17Z")

</div>

Hello @quark,

This is your code on W10 Processing 4.5.2:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/f/1fd8f40d7fa3e01dd3f952108c555cc8a9d9f142.png)

My desktop background is orange.  
This may very well depend on the OS.

Is it still the same output for you now with Processing 4.5.2?

Related:  
_[How do I create a fullscreen window with title bar on my second monitor?](https://discourse.processing.org/t/how-do-i-create-a-fullscreen-window-with-title-bar-on-my-second-monitor/41474)_

`:)`

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [April 14, 2026, 6:59am UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/11 "2026-04-14T06:59:53Z")

</div>

> [@glv](#):
>
> Is it still the same output for you now with Processing 4.5.2?

Just tested it with 4.5.1 on Osx 15.7.4 (latest) and I got the same as I did 3 years ago 😁

I suspect it is either something to do with Windows 10 or how Processing interacts with Windows 10

---

<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: [April 14, 2026, 7:23pm UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/12 "2026-04-14T19:23:36Z")

</div>

> [@quark](#):
>
> I suspect it is either something to do with Windows 10 or how Processing interacts with Windows 10

I did some further exploration here: [Native window access and inset handling in Processing JAVA2D and P2D](https://discourse.processing.org/t/native-window-access-and-inset-handling-in-processing-java2d-and-p2d/48437)

`:)`

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [April 15, 2026, 12:56am UTC](https://discourse.processing.org/t/using-surface-setlocation-does-not-do-same-thing-every-time/11871/13 "2026-04-15T00:56:55Z")

</div>

`I suspect it is either something to do with Windows 10 or how Processing interacts with Windows 10`

Windows 11 is afflicted as well:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/0/d/0dc23a337d8901a81b2d5ab1745beb9f43134145.png)
