【问题标题】:(Processing) PGraphics doesn't update when all pixels copied from another PGraphics(处理中)当从另一个 PGraphics 复制所有像素时,PGraphics 不会更新
【发布时间】:2013-05-17 09:22:14
【问题描述】:

我对 PGraphics 有一个小问题。我有以下从我正在处理的大型项目中提取的代码:

int x=0;
int y=0;

PGraphics array1;
PGraphics array2;

void setup() {
  size(200,200);
  background(0);
  array1 = createGraphics(200,200);
  array2 = createGraphics(200,200);
  frameRate(10);
}

void draw() {
  array1.beginDraw();
  array1.background(0);
  array1.noStroke();
  array1.fill(150);
  array1.ellipse(x,y,20,20);
  array1.endDraw();

  array1.loadPixels();
  array2.loadPixels();

  //presence of two below lines doesn't change anything
  array1.updatePixels();
  array2.updatePixels();

  //not by arrayCopy since I want to add filters in next project
  for(int i=0; i<200*200; i++) {
    array2.pixels[i] = array1.pixels[i];
  }

  //presence of two below lines doesn't change anything
  array1.updatePixels();
  array2.updatePixels();

  //I want to see only the array2 - now it should be the same as array1
//  image(array1, 0,0);
  image(array2, 0,0);

  //presence of two below lines doesn't change anything
  array1.updatePixels();
  array2.updatePixels();

  x++;
  y++;

}

一般来说,我想将 PGraphics array1 复制到 PGraphics array2 中。但是,array2 看起来像第一帧中的 array1,并且视觉效果不会更新。当我添加时:

println(array1.pixels[0]+" "+array2.pixels[0]);

打印出来:

...
-6908266 -6908266
-6908266 -6908266
-6908266 -6908266
-16777216 -16777216
-16777216 -16777216
...

所以显然两个数组都包含相同的值。我不知道为什么我看不到更新后的array2。

按照文档,我尝试了将 updatePixels 方法放置在不同的地方,但这没有帮助。

我错过了什么?

提前致谢!

【问题讨论】:

    标签: processing pixels pgraphics


    【解决方案1】:

    您忘记为 array2 PGraphic 调用 beginDraw() 和 endDraw()。如果你这样做:

    array2.beginDraw(); // HERE!!
      for (int i= 0; i<array1.pixels.length; i++) {
        array2.pixels[i] = array1.pixels[i];
      }
      array2.updatePixels();
    array2.endDraw(); // and HERE!! :)
    

    它应该按预期工作。

    【讨论】:

    • 谢谢!我知道这一定很简单……完美!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多