About sketch size and position

edited April 2017 in Share Your Work

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

  • edited April 2017

    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() and getLocation() 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

    import processing.awt.PSurfaceAWT.SmoothCanvas;
    import com.jogamp.newt.opengl.GLWindow;
    
    PVector getSize() {
      return getSize(this);
    }
    
    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) {
        System.err.println("Unable to retrieve window size");
        return new PVector(0, 0);
      }
    }
    
    PVector getLocation() {
      return getLocation(this);
    }
    
    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) {
        System.err.println("Unable to retrieve window location");
        return new PVector(0, 0);
      }
    }
    
  • Thanks for the correction. I think I got FX2D, so to all renderers, the code will be like

    import processing.awt.PSurfaceAWT.SmoothCanvas;
    import com.jogamp.newt.opengl.GLWindow;
    import processing.javafx.PSurfaceFX;
    import javafx.scene.canvas.Canvas;
    
    PVector getSize() {
      return getSize(this);
    }
    
    PVector getSize(PApplet applet) {
        if(applet.getGraphics().isGL())
          return new PVector(
            ((GLWindow) applet.getSurface().getNative()).getWidth(),
            ((GLWindow) applet.getSurface().getNative()).getHeight()
          );
        else
          try {
            return new PVector(
              ((SmoothCanvas) applet.getSurface().getNative()).getFrame().getWidth(),
              ((SmoothCanvas) applet.getSurface().getNative()).getFrame().getHeight()
            );
          }
          catch (Exception e) {
            return new PVector(
              (int) ((Canvas) applet.getSurface().getNative()).getScene().getWindow().getWidth(),
              (int) ((Canvas) applet.getSurface().getNative()).getScene().getWindow().getHeight()
            );
          }
    }
    
    PVector getLocation() {
      return getLocation(this);
    }
    
    PVector getLocation(PApplet applet) {
      if(applet.getGraphics().isGL())
        return new PVector(
          ((GLWindow) applet.getSurface().getNative()).getX(),
          ((GLWindow) applet.getSurface().getNative()).getY()
        );
      else
      try {
        return new PVector(
          ((SmoothCanvas) applet.getSurface().getNative()).getFrame().getX(),
          ((SmoothCanvas) applet.getSurface().getNative()).getFrame().getY()
        );
      }
      catch (Exception e) {
        return new PVector(
          (int) ((Canvas) applet.getSurface().getNative()).getScene().getWindow().getX(),
          (int) ((Canvas) applet.getSurface().getNative()).getScene().getWindow().getY()
        );
      }
    }
    
Sign In or Register to comment.