0

这会在运行几秒钟后产生 OutOfMemory 异常。有任何想法吗?

PGraphics img;

void setup() {
  size(500, 500);
  img = createGraphics(width, height, JAVA2D);

  // this is here just for the testcase because else I get a
  // NullPointerException too (probably a harmless Processing bug)
  img.beginDraw(); img.endDraw();
}

void draw() {
  PGraphics tmpImg = createGraphics(img.width, img.height, JAVA2D);
  tmpImg.beginDraw();
  tmpImg.image(img, 0, 0);
  tmpImg.endDraw();
  tmpImg.dispose();
}
4

1 回答 1

2

Mat 是对的,您不应该每帧都实例化一个新的 PGraphics。你可以简单地做这样的事情:

PGraphics img;

void setup() {
  size(500, 500);
  img = createGraphics(width, height, JAVA2D);

  // this is here just for the testcase because else I get a
  // NullPointerException too (probably a harmless Processing bug)
  img.beginDraw(); img.endDraw();
}

void draw() {
  image(img, 0, 0);
}

因为PGraphics扩展了 PImage。通常你会使用基本的 PImage API,但如果你需要在位图或假“图层”上绘制形状,你可以将 PGraphics 与 PImage 结合使用,但不要分配新的 PGraphics 30 到 60 次一秒。

于 2012-05-20T05:56:15.117 回答