我对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,并且visual不会更新。当我添加:
println(array1.pixels[0]+" "+array2.pixels[0]);
打印:
...
-6908266 -6908266
-6908266 -6908266
-6908266 -6908266
-16777216 -16777216
-16777216 -16777216
...
显然两个数组都包含相同的值。我不知道为什么我看不到更新的array2。
按照文档我尝试将updatePixels方法放在不同的地方,但这没有帮助。
我缺少什么?
提前致谢!
答案 0 :(得分:3)
你忘了为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!! :)
它应该按预期工作。