How can I get the location of a PApplet window?
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();
smoothCanvas.getFrame().getLocation(p);
background(0);
textSize(60);
text(p.x + " " + p.y, 50, 50);
}
if anyone’s curious about p5.js (javascript) version
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)
}
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 )
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…!
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() {
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
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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setLocationRelativeTo(null);
x = frame.getLocation().x;
y = frame.getLocation().y;
println(x, y);
}
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;
}
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?
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.