Why does "drawing under" work in JS but not in JAVA?

edited November 2013 in Questions about Code

I just found some old code which does not work right in the standard Processing, but it works in PJS for some reason. I always wondered what's wrong with it. Someone knows? The idea is simple: alternate between two PGraphics. Draw on "curr", place "other" on top, switch. The goal is to draw below previous shapes.

PGraphics[] pg = new PGraphics[2];
void setup() {
  size(500, 500);
  for (int i=0; i<pg.length; i++) {
    pg[i] = createGraphics(width, height);
    pg[i].beginDraw();
    pg[i].endDraw();
  }
}
void draw() {
  PGraphics curr = pg[frameCount % 2];
  PGraphics other = pg[1-(frameCount % 2)];
  curr.beginDraw();
  curr.background(0, 0);
  curr.fill(random(255));
  curr.rect(random(width), random(height), 50, 50);
  // draw other pg on top of the current pg
  curr.image(other, 0, 0);
  curr.endDraw();
  background(0);
  image(curr, 0, 0);
}

Here you can run an example of this: http://hamoid.com/code/crvy/

Tagged:

Answers

  • I found a solution. Use copy() instead of image(). Replace curr.image(other, 0, 0); with curr.copy(other, 0, 0, width, height, 0, 0, width, height);

    Why this works? I don't know.

Sign In or Register to comment.