We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › PGraphics to PImage Transparency
Page Index Toggle Pages: 1
PGraphics to PImage Transparency (Read 1848 times)
PGraphics to PImage Transparency
Jan 17th, 2010, 12:59pm
 
I've got some sample code that I'm trying to get working. I want to be able to draw on a layer above the g PGraphics object, then save it to a PImage, then display it on demand. I've got the layer transparency working...I can draw to my new PGraphics layer, and toggle it on and off. But, when I save it to a PImage, then try to display the PImage, it is solid black. Within the program, I want to be able to draw some lines, press 's', press 'd', then press 'a' and be looking at the same image. Can anyone help?

Code:
PGraphics pg;

PImage frame;

boolean displayOverlay;
boolean displayFrame;

void setup() {
 size(600, 600, JAVA2D);
 background(230);
 fill(255, 0, 0);
 stroke(0);
 strokeWeight(6);
 
 pg = createGraphics(600, 600, JAVA2D);
 pg.background(230);
 pg.noFill();
 pg.stroke(0);
 pg.strokeWeight(6);
 
 displayOverlay = true;
 displayFrame = false;
 
 frame = createImage(600, 600, ARGB);
}

void draw() {
 background(230);
 rect(250, 250, 100, 100);
 
 pg.beginDraw();
 if(mousePressed)
   pg.line(mouseX, mouseY, pmouseX, pmouseY);
 pg.endDraw();
 
 if(displayOverlay == true)
   image(pg, 0, 0);
 if(displayFrame == true)
   image(frame, 0, 0);
}

void keyPressed() {
 if(key == 'd') {
   displayOverlay = !displayOverlay;
   println("Toggling overlay.");
 }
 
 if(key == 's') {
   frame = pg.get();
   println("Saving overlay.");
 }
 
 if(key == 'a') {
   displayFrame = !displayFrame;
   println("Toggling frame.");
 }
}


Re: PGraphics to PImage Transparency
Reply #1 - Jan 17th, 2010, 2:54pm
 
Basically, I don't know why the get() doesn't work...
But well, I could make your code to work.
Notes:
- For pg to work as a transparent overlay, you must not put a background() call in it.
- You must wrap the pg.xxx() calls with beginDraw/endDraw.
- I used a PGraphics instead of a PImage for frame, so I can use image() instead of get().
Code:
PGraphics pg;
PGraphics frame;

boolean displayOverlay = true;
boolean displayFrame;

void setup() {
size(600, 600, JAVA2D);
background(230);
fill(255, 0, 0);
stroke(0);
strokeWeight(6);

pg = createGraphics(width, height, JAVA2D);
pg.beginDraw();
pg.noFill();
pg.stroke(0);
pg.strokeWeight(6);
pg.endDraw();

frame = createGraphics(width, height, JAVA2D);
}

void draw() {
background(230);
rect(250, 250, 100, 100);

if (mousePressed && displayOverlay) {
pg.beginDraw();
pg.line(mouseX, mouseY, pmouseX, pmouseY);
pg.endDraw();
}

if (displayOverlay)
image(pg, 0, 0);
if (displayFrame)
image(frame, 0, 0);
}

void keyPressed() {
if (key == 'd') {
displayOverlay = !displayOverlay;
println("Toggling overlay.");
}

if (key == 's') {
frame.beginDraw();
frame.image(pg, 0, 0);
frame.endDraw();
println("Saving overlay.");
}

if (key == 'a') {
displayFrame = !displayFrame;
println("Toggling frame.");
}
}
Re: PGraphics to PImage Transparency
Reply #2 - Jan 17th, 2010, 8:44pm
 
Thanks for the help! I was able to get the parent code working. Having so many PGraphics is using up a lot of memory (PImage would be the same though). I will try to think of a more efficient way. Now I am trying to implement the onion skin component through tinting. I've pasted the code below; the only real change is that I try to tint the "frame" PGraphics with .tint(255, 0, 0, 128) It doesn't seem to be working.

Code:
PGraphics pg;
PGraphics frame;

boolean displayOverlay;
boolean displayFrame;

void setup() {
 size(600, 600, JAVA2D);
 background(50);
 fill(255, 0, 0);
 stroke(255);
 strokeWeight(6);
 
 pg = createGraphics(width, height, JAVA2D);
 pg.beginDraw();
 pg.noFill();
 pg.stroke(255);
 pg.strokeWeight(6);
 pg.endDraw();
 
 displayOverlay = true;
 displayFrame = false;
 
 frame = createGraphics(width, height, JAVA2D);
}

void draw() {
 background(50);
 rect(250, 250, 100, 100);
 
 if (mousePressed) {
   pg.beginDraw();
   pg.line(mouseX, mouseY, pmouseX, pmouseY);
   pg.endDraw();
 }
 
 if(displayOverlay)
   image(pg, 0, 0);
 if(displayFrame)
   image(frame, 0, 0);
}

void keyPressed() {
 if (key == 'd') {
   displayOverlay = !displayOverlay;
   println("Toggling overlay.");
 }
 
 if (key == 's') {
   frame.beginDraw();
   frame.image(pg, 0, 0);
   frame.tint(255, 0, 0, 128);
   frame.endDraw();
   println("Saving overlay.");
 }
 
 if (key == 'a') {
   displayFrame = !displayFrame;
   println("Toggling frame.");
 }
}
Re: PGraphics to PImage Transparency
Reply #3 - Jan 28th, 2010, 9:50pm
 
I got tint() working, but only for the default "g" PGraphics object. It then tints the images that I display after it...but it tints all of them. And, if I reset the tint to tint(255, 255) it makes all the images opaque. Can you not tint an off-screen PGraphics?
Re: PGraphics to PImage Transparency
Reply #4 - Jan 28th, 2010, 11:05pm
 
I think I figured it out. I tinted the onion-skin layer before calling .image() and saving the old paint layer to it. This is from my actual program, not the test program, so the variable/function names are different.

Code:
void drawOnionLayer() {
onionLayer.beginDraw();
onionLayer.background(backgroundColor); //Clear the last onion-skin
PGraphics buffer = (PGraphics) frames.get(frames.size() - 1); //Add the latest painted frame to the onion-skin layer
onionLayer.tint(255, 128); //This does the tinting
onionLayer.image(buffer, 0, 0); //This displays it, tinted
onionLayer.endDraw();
}
Page Index Toggle Pages: 1