Get location of a PApplet window

PVector getWindowLocation() {

  PVector windowLocation = new PVector();

  switch (sketchRenderer()) {
  case P2D:
  case P3D:
    com.jogamp.nativewindow.util.Point p = new com.jogamp.nativewindow.util.Point();
    ((com.jogamp.newt.opengl.GLWindow) surface.getNative()).getLocationOnScreen(p);
    windowLocation.x = p.getX();
    windowLocation.y = p.getY();
    break;
  case FX2D:
    final processing.javafx.PSurfaceFX FXSurface = (processing.javafx.PSurfaceFX) surface;
    final javafx.scene.canvas.Canvas canvas = (javafx.scene.canvas.Canvas) FXSurface.getNative();
    final javafx.stage.Stage stage = (javafx.stage.Stage) canvas.getScene().getWindow();
    windowLocation.x = (float) stage.getX();
    windowLocation.y = (float) stage.getY();
    break;
  case JAVA2D:
    java.awt.Frame f = (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative())
        .getFrame();
    windowLocation.x = f.getX();
    windowLocation.y = f.getY();
    break;
  }

  return windowLocation;
}
3 Likes