Get location of a PApplet window

How can I get the location of a PApplet window?

1 Like

It seems way more complicated than you imagine…

(how I found it was I looked at my old sketch that uses setLocation so I guessed there would be getLocation)

import processing.awt.PSurfaceAWT;

PSurfaceAWT.SmoothCanvas smoothCanvas;
void setup() {
  size(400, 400);
  PSurfaceAWT awtSurface = (PSurfaceAWT)surface;
  smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();

void draw() {
  java.awt.Point p = new java.awt.Point();
  text(p.x + " " + p.y, 50, 50);
1 Like

if anyone’s curious about p5.js (javascript) version

function setup() {
  createCanvas(400, 400);

function draw() {
  const W = window;
  text(`window size: ${W.innerWidth},${W.innerHeight}`,50,50)
  text(`window position: ${W.screenX},${W.screenY}`,50,80)
  text(`display size: ${W.screen.availWidth},${W.screen.availHeight}`,50,110)
1 Like

PSurfaceJOGL can’t be cast to PSurfaceAWT class.

java.lang.ClassCastException: processing.opengl.PSurfaceJOGL cannot be cast to processing.awt.PSurfaceAWT

sure. Do you want to share your environment so we have more clues to fix the problem? I tried it on processing 3.5.4 with windows 10 and ubuntu and both worked.

(by the way we are not a product support team but volunteers trying to help you - so we appreciate it if you add a bit more words :slight_smile: )

1 Like

Sure! I use Windows 10 20H2 (2004) and I use processing 3.5.4. I don’t use processing IDE I use processing on intelliJ idea 2020.3

thanks! actually I noticed that you are using opengl (P3D) and that’s why the returned object is different and giving cast error. i don’t have time to fix it right now but hopefully it helps…!

1 Like

Hi @yeppii ,

You could try using the GLWindow from the PSurfaceJOGL?

import com.jogamp.newt.opengl.GLWindow;

GLWindow window;

void setup() {
  size(720, 405, P3D);
  window = (GLWindow)getSurface().getNative();

void draw() {
  camera(0.0, 0.0, height * 0.86602, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);


There are also window insets that you can acquire from the window if you need to further refine the position.



To find the location of a PApplet window, you have to understand that a JFrame is a java class that creates a window. My way of getting the position is by making a new JFrame within setup, getting the position of it, and printing the position.

import javax.swing.*;
JFrame frame;
int x, y;
void setup() {
  JFrame frame = new JFrame();
  frame.setSize(width, height);
  x = frame.getLocation().x;
  y = frame.getLocation().y;
  println(x, y);

1 Like
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();
  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();
  case JAVA2D:
    java.awt.Frame f = (java.awt.Frame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative())
    windowLocation.x = f.getX();
    windowLocation.y = f.getY();

  return windowLocation;

I am actually using the IntelliJ idea, so some features that are available in Processing IDE (like float numbers without f postfix) don’t work. Can you optimize the code for using with IntelliJ idea?

1 Like

I actually need a way to center the mouse cursor and be able to move queasycam at the same time, so I thought editing queasycam code a bit to make it work.

1 Like