Getting A Sketch's Window's Position

Hello,
I’ve been searching for an answer to this for a while now and have been unable to find any solutions that work for the most recent versions of processing. Does anyone know how you would get the X and Y coordinates of the processing sketch window relative to your computer screen. I am using the windows version of processing and the P3D renderer.
Thanks!

1 Like

I guess it really depends on your renderer. Usually you can access the underlaying canvas and window by using the surface variable. Using getNative(), it is possible to get the current implementation, which by default is PSurfaceAWT.SmoothCanvas. If you would use FX2D renderer, this would be another type of window. And from there you can get the swing frame which holds the coordinates.

Here an example sketch for the default renderer which works in Processing4:

import javax.swing.*;
import processing.awt.*;

void setup() {
  size(500, 500);
}

void draw() {
  background(55);

  readWindowPosition();
}

void readWindowPosition() {
  PSurfaceAWT.SmoothCanvas canvas = (PSurfaceAWT.SmoothCanvas)surface.getNative();
  JFrame frame = (JFrame)canvas.getFrame();
  println("Position: ", frame.getX(), frame.getY());
}

The challenge is to make this render-independent.

2 Likes

sorry I probably should have been more specific in my original post. I am using the P3D renderer, which this code doesn’t seem to work on. Do you know any solutions that would work in P3D?

I am not at a computer atm. But I recommend you to just start with the getNative() method. Print the result out into the console and you‘ll get the actual type of it. Then look it up on google and check out it‘s properties and methods. And if one sounds promising, repeat the step.

That‘s how I would have to find it out too.

the P2D and P3D use OpenGL so we have GLWindow instead of JFrame.

Run this sketch and when you click the mouse the window position is printed out.

void setup() {
  size(300, 200, P3D);
}

void draw() {
  background(0);
}

void mouseClicked() {
  PVector p = getPosition(null);
  println(p);
}

// Get the GL window for this sketch
public com.jogamp.newt.opengl.GLWindow getCanvas() {
  return (com.jogamp.newt.opengl.GLWindow) surface.getNative();
}

// Get the window position.
public PVector getPosition(PVector pos) {
  if (pos == null)
    pos = new PVector();
  pos.x = getCanvas().getX();
  pos.y = getCanvas().getY();
  return pos;
}

3 Likes
3 Likes

Just adding a note for people deciding on whether to go the getNative() route: not only is it a renderer specific Object – a whatever-the-renderer-gives-us – but it could be subject to change. This is whimsically expressed in the Processing API documentation…

from PSurface.getNative :

Get the native window object associated with this drawing surface. For Java2D, this will be an AWT Frame object. For OpenGL, the window. The data returned here is subject to the whims of the renderer, and using this method means you’re willing to deal with underlying implementation changes and that you won’t throw a fit like a toddler if your code breaks sometime in the future.

2 Likes