How to get a snapshot of a PGraphics "paused" frame and use it as a PImage at a predefined condition in time?

If I use static sketches I can create a PImage right away and use it in a PGraphics buffer, then use it as a BufferedImage successfully with getNative().
How do I get a snapshot of this PGraphics buffer in time if I have a moving/animated object and a sketch with frames? This snapshot (a future frame) can be a “pause” of the animation in time or if a condition is met. e.g.:
“if frame count=X then pause”
“if a condition is met then pause”
then get the PImage

With static sketches I get the one and only frame at the end. But how could I get a specified frame in time (after x seconds or minutes) or if a condition is met? Does this have to do something with the delay() method?. How do I pause the animation or “movement” and then get a PGaraphics/PImage at this very moment?

the PImage save() serve a similar purpose, but I do not need an image file, I just need to pass the PGaraphics/PImage buffer “paused” object to getNative().

thanks!

Hello.
You don’t really need to “pause”

PImage buff;
float now;
float interval = 4530;

void setup() {
  size(400, 400);
  buff = createImage(400, 400, RGB);
  now = millis();
}

void draw() {
  background((millis()/10)%255, 180, ( millis()/8)%255);
  noStroke();
  for (int i = 0; i< 15; i++) {
    float x = random(width);
    float y = random(height);
    float r = random(30);
    color c = color(x%255, y%255, 180);
    fill(c); 
    ellipse(x, y, r, r);
  }




  if (millis() - now > interval) {
    save();
    now = millis();
  }

  image(buff, 0, 0, width/2, width/2);
}

void mouseClicked() {
  save();
}

void save() {
  loadPixels();
  buff.loadPixels();
  for (int i =0; i< pixels.length; i++) {
    buff.pixels[i]  = pixels[i];
  }
  updatePixels();
  buff.updatePixels();
}

But if you want to you can. Look in the reference for noLoop(),loop() and redraw().

2 Likes

excellent example, straight to the point reply. I will try to use those “buffer save” timings in a multi instance sketch environment that expose multiple buffers. hopefully with some tweaks it will work… I will mark as a solution. Thanks a lot !

2 Likes

This solution is great when using processing export / save inside sketch. But if I try to get the image form an external java class with getNative() and pass it as a bufferedimage, I always get the 1st frame rendered only. If sketch is noloop that is fine, but if animated, I wish I had the option to get the image at a specific frame in time.

Is there an effective way to use the getNative() method and get a result after x time, or x frames?
Do I have to override getNative() in order to pass a “time/frame” parameter like this one?

Both of the methods return only 1st frame:

static final BufferedImage grabBufferedImage (PImage pi) {
  //return new BufferedImage, Casting with getNative); 
    BufferedImage bi = (BufferedImage) pi.getNative();
  return bi;
}
public static  BufferedImage pi2bi(PImage pi) {
  BufferedImage bi = new BufferedImage(pi.width, pi.height, BufferedImage.TYPE_INT_ARGB);
  pi.loadPixels();
  bi.setRGB(0, 0, pi.width, pi.height, pi.pixels, 0, pi.width);
  return bi;
}