# Get location of a PApplet window

**URL:** <https://discourse.processing.org/t/get-location-of-a-papplet-window/28039>\
**Category:** Coding Questions\
**Created:** [February 24, 2021, 6:45am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039 "2021-02-24T06:45:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [February 24, 2021, 6:45am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/1 "2021-02-24T06:45:20Z")

</div>

How can I get the location of a PApplet window?

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [February 24, 2021, 7:31am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/2 "2021-02-24T07:31:46Z")

</div>

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

```java
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();
  smoothCanvas.getFrame().getLocation(p);
  background(0);
  textSize(60);
  text(p.x + " " + p.y, 50, 50);
}

```

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [February 24, 2021, 7:37am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/3 "2021-02-24T07:37:11Z")

</div>

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

```auto
function setup() {
  createCanvas(400, 400);
}

function draw() {
  background(220);
  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)
}

```

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [February 24, 2021, 8:51am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/4 "2021-02-24T08:51:07Z")

</div>

> [@micuat](#):
>
> ```auto
> java.awt.Point p = new java.awt.Point();
> smoothCanvas.getFrame().getLocation(p);
> 
> ```

PSurfaceJOGL can’t be cast to PSurfaceAWT class.

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

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [February 24, 2021, 10:36am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/5 "2021-02-24T10:36:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [February 24, 2021, 10:46am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/6 "2021-02-24T10:46:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [February 24, 2021, 10:48am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/7 "2021-02-24T10:48:22Z")

</div>

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…!

---

<div class="post-metadata">

**Author:** ![behreajj](https://avatars.discourse-cdn.com/v4/letter/b/b5a626/32.png) [@behreajj](https://discourse.processing.org/u/behreajj)\
**Post date:** [February 24, 2021, 7:09pm UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/8 "2021-02-24T19:09:04Z")

</div>

Hi @yeppii ,

You could try using the `GLWindow` from the `PSurfaceJOGL`?

```auto
import com.jogamp.newt.opengl.GLWindow;

GLWindow window;

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

void draw() {
  background(#202020);
  lights();
  camera(0.0, 0.0, height * 0.86602, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

  println(window.getX());
  println(window.getY());
}

```

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

Best,  
Jeremy

---

<div class="post-metadata">

**Author:** ![SNICKRS](https://avatars.discourse-cdn.com/v4/letter/s/43a26b/32.png) [@SNICKRS](https://discourse.processing.org/u/SNICKRS)\
**Post date:** [February 24, 2021, 11:20pm UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/9 "2021-02-24T23:20:13Z")

</div>

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.

```auto
import javax.swing.*;
JFrame frame;
int x, y;
void setup() {
  JFrame frame = new JFrame();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setSize(width, height);
  frame.setLocationRelativeTo(null);
  x = frame.getLocation().x;
  y = frame.getLocation().y;
  println(x, y);
}

```

---

<div class="post-metadata">

**Author:** ![micycle](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micycle/32/201_2.png) [@micycle](https://discourse.processing.org/u/micycle)\
**Post date:** [February 25, 2021, 2:25pm UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/10 "2021-02-25T14:25:25Z")

</div>

> <https://stackoverflow.com/questions/65600999/need-a-java-processing-class-for-moving-mouse-to-window-center/65603498#65603498>

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 10:38am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/11 "2021-03-04T10:38:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![yeppii](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/yeppii/32/12180_2.png) [@yeppii](https://discourse.processing.org/u/yeppii)\
**Post date:** [March 4, 2021, 10:40am UTC](https://discourse.processing.org/t/get-location-of-a-papplet-window/28039/12 "2021-03-04T10:40:00Z")

</div>

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.
