Using surface.setLocation does not do same thing every time

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.

Hey There!

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


  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.

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.)

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);
  }
}

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.