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.

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

surface.setLocation(0, 0);

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)

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

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

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.

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

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