I have a small problem with PGraphics. I have folowing piece of code extracted from larger project I am working on:
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++;
}
In general I want to copy PGraphics array1 into PGraphics array2. However the array2 looks like the array1 in the first frame and the visual doesn't update. When I have added:
println(array1.pixels[0]+" "+array2.pixels[0]);
it prints:
...
-6908266 -6908266
-6908266 -6908266
-6908266 -6908266
-16777216 -16777216
-16777216 -16777216
...
So obviously both arrays contain the same value. I have no idea why I can't see the updated array2.
Following the docs I tried the updatePixels method placed in different places and this doesn't help.
What I am missing?
Thanks in advance!
You forgot to call beginDraw() and endDraw() for array2 PGraphic. If you do:
array2.beginDraw(); // HERE!!
for (int i= 0; i<array1.pixels.length; i++) {
array2.pixels[i] = array1.pixels[i];
}
array2.updatePixels();
array2.endDraw(); // and HERE!! :)
it should work as expected.