Draw one PGraphics into another?

edited March 2014 in How To...

I was inspired by this video of sand art on a potter's wheel by KRUGOVOROT, and want to see if I can make something similar in Processing. I need to be able to draw on a canvas, then rotate it before drawing the next frame.

I don't quite have my algorithm sorted out yet, but I'm pretty sure I need to draw one PGraphics buffer into another to pull this off. In my test code below, though, buffer.image(oldbuffer, 51, 30); produces a null pointer exception on the very first pass.

How can I do this? Am I going about it all wrong?

PGraphics buffer;
PGraphics oldbuffer;

void setup() {
  size(100, 100);
  buffer = createGraphics(width, height);
  oldbuffer = createGraphics(width, height);
}

void draw() {
  buffer.beginDraw();
  rotate(0.1);
  buffer.image(oldbuffer, 51, 30);
  buffer.ellipse(mouseX, mouseY, 10, 10);
  buffer.endDraw();
  image(buffer, 0, 0);
  oldbuffer = buffer;
}
Tagged:

Answers

  • edited March 2014

    After instantiating a PGraphics w/ createGraphics(), it still needs beginDraw() before start using it! :-@

  • I think the rotate() call should be prefixed by buffer. too. And the first time draw() is called, oldbuffer isn't drawn on yet, so it is in a state where it cannot be drawn. As GoToLoop said, you need to add:

    oldbuffer.beginDraw();
    oldbuffer.endDraw();
    

    at the end of setup() to make it work.

Sign In or Register to comment.