We are about to switch to a new forum software. Until then we have removed the registration on this forum.
There are a lot of questions about how to manage the sketch dimensions. It doesn't help that Processing 3 makes a lot of (not so) old solutions useless. Thanks to @gotoloop, @quark and others experts some of us can still get what we want. That's my case. I know that surface provide setSize and setLocation, but sometimes I like to know the sketch or a G4P window location. So I wrote this helpers. I'm posting in search of approval/criticism of those who knows a lot more then I do.
import processing.awt.PSurfaceAWT.SmoothCanvas;
import com.jogamp.newt.opengl.GLWindow;
PVector getSize() {
try {
if(getGraphics().isGL())
return new PVector(
((GLWindow) surface.getNative()).getWidth(),
((GLWindow) surface.getNative()).getHeight()
);
else
return new PVector(
((SmoothCanvas) surface.getNative()).getFrame().getWidth(),
((SmoothCanvas) surface.getNative()).getFrame().getHeight()
);
}
catch (Exception e)
{
return new PVector(0, 0);
}
}
PVector getSize(PApplet applet) {
try {
if(applet.getGraphics().isGL())
return new PVector(
((GLWindow) applet.getSurface().getNative()).getWidth(),
((GLWindow) applet.getSurface().getNative()).getHeight()
);
else
return new PVector(
((SmoothCanvas) applet.getSurface().getNative()).getFrame().getWidth(),
((SmoothCanvas) applet.getSurface().getNative()).getFrame().getHeight()
);
}
catch (Exception e)
{
return new PVector(0, 0);
}
}
PVector getLocation() {
try {
if(getGraphics().isGL())
return new PVector(
((GLWindow) getSurface().getNative()).getX(),
((GLWindow) getSurface().getNative()).getY()
);
else
return new PVector(
((SmoothCanvas) getSurface().getNative()).getFrame().getX(),
((SmoothCanvas) getSurface().getNative()).getFrame().getY()
);
}
catch (Exception e) {
return new PVector(0, 0);
}
}
PVector getLocation(PApplet applet) {
try {
if(applet.getGraphics().isGL())
return new PVector(
((GLWindow) applet.getSurface().getNative()).getX(),
((GLWindow) applet.getSurface().getNative()).getY()
);
else
return new PVector(
((SmoothCanvas) applet.getSurface().getNative()).getFrame().getX(),
((SmoothCanvas) applet.getSurface().getNative()).getFrame().getY()
);
}
catch (Exception e) {
return new PVector(0, 0);
}
}
Comments
The G4P window class does have a getPosition() method which works for JSwing and OpenGL windows although it doesn't have a get size option.
In the code above the no parameter methods
getPosition()
andgetLocation()
must be called from inside a PApplet object if they are to compile. It might be nice to display a red error message in the console.So taking these factore into account I suggest
Thanks for the correction. I think I got FX2D, so to all renderers, the code will be like