getLocation() of the sketch

I try to catch the location of my sketch, but I don’t find any option to do that like surface.setLocation(). Any idea which method can be used to catch the sketch position, like before getLocationOnScreen()

The PSurface class doesn’t expose the location. You’ll have to go through the native window object. This is going to depend on which renderer you’re use.

I’d start here:

https://processing.github.io/processing-javadocs/core/processing/core/PSurface.html

I want something universel, work with P2D, P3D, JAVAFAX, JAVA2D… but I don’t find anything to catch the window object :frowning:

1 Like

Perfect, thx that’s work like a charm !

int get_sketch_location_x() {
  return getJFrame(getSurface()).getX();
}

int get_sketch_location_y() {
  return getJFrame(getSurface()).getY();
}

 
static final javax.swing.JFrame getJFrame(final PSurface surface) {
  return (javax.swing.JFrame) ( (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
}
1 Like

improve the detection for the case the sketch is in P2D or P3D

int get_sketch_location_x() {
  if(get_renderer() != P3D && get_renderer() != P2D) {
    return getJFrame(getSurface()).getX();
  } else {
    return get_rectangle(surface).getX();

  }
  
}

int get_sketch_location_y() {
  if(get_renderer() != P3D && get_renderer() != P2D) {
    return getJFrame(getSurface()).getY();
  } else {
    return get_rectangle(surface).getY();
  }
}


com.jogamp.nativewindow.util.Rectangle get_rectangle(PSurface surface) {
  com.jogamp.newt.opengl.GLWindow window = (com.jogamp.newt.opengl.GLWindow) surface.getNative();
  com.jogamp.nativewindow.util.Rectangle rectangle = window.getBounds();
  return rectangle;
}


static final javax.swing.JFrame getJFrame(final PSurface surface) {
  return (javax.swing.JFrame)
  (
    (processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()
  ).getFrame();
}
1 Like