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 › Basic help with Alpha behaviour
Page Index Toggle Pages: 1
Basic help with Alpha behaviour (Read 549 times)
Basic help with Alpha behaviour
Apr 2nd, 2009, 4:31pm
 
Hi people, I'm new on Processing! Just discovered it few months ago, and I'm liking it a lot!
Well, I wanted to ask you a question about a simple program. I try to draw a square by drawing pixels instead of using the rect function.
Everything goes fine as long as I try to draw them with a "normal" colour. But whenever I try to apply some alpha to the colour I want to draw, something unexpected – at least for me – happens:
It applies an extra amount of alpha to the previously drawn squares, having as consequence that the squares that were drawn in the beginning end up losing any transparency that they were supposed to have.
I don't know if I've explained myself in a correct way. As I've said, I'm new on this, so my apologies if I've done something wrong!
Here I paste the code, in case it sheds a light on the whole thing! Any help is really appreciated!

Code:

void setup(){
 size(400,300);
 noLoop();
}
void draw(){
 int x = mouseX;
 int y = mouseY;
 
 color c = color(0,0,0);
 c = color(c,25); // Here I apply the alpha
 
 // 30 px wide square
 for(int i = x-15; i < x+15; i++)
   for(int j = y-15; j < y+15; j++)
     set(i, j, c);
}
void mouseClicked(){
 redraw();
}
void mouseDragged(){
 redraw();
}


So any ideas about how could the transparency be kept?
Thanks for everything in advance!
Re: Basic help with Alpha behaviour
Reply #1 - Apr 2nd, 2009, 7:16pm
 
hmm strange,
I changed some parts of your code, cause you dont need all that noLoop() and redrawing()...

anyway I cant explain why it behaves that way.
btw i changed set to point and just draw some points but it still doesnt work with the JAVA2D renderer... but if you change size to size(400,300,P2D); it seems to work the way you want it...
dont know why, maybe a rendering bug.

Does anybody know why ?


color c = color(0,0,0,25);
void setup(){
 size(400,300,P2D);
 stroke(c);
}
void draw(){
}

void mouseDragged(){
 int x = mouseX;
 int y = mouseY;

 for(int i = x-15; i < x+15; i++){
   for(int j = y-15; j < y+15; j++){
     point(i,j);
   }
 }
}
Re: Basic help with Alpha behaviour
Reply #2 - Apr 3rd, 2009, 2:46am
 
My interpretation: you alter the image that Processing uses to draw the sketch. But each time you call redraw, Processing draws this image on the Java screen buffer, using transparency information, and Java merges the given image with the one it has. Thus, a previously drawn square is progressively darkened until it becomes black.

I am not sure I am very clear - and my interpretation can be wrong! Smiley

But I simulated such behavior using your code:
Code:
PGraphics drawingSurface;

void setup(){
size(400,300);
drawingSurface = createGraphics(400, 300, JAVA2D);
drawingSurface.loadPixels(); // Only to avoid NPE
noLoop();
}
void draw(){
// background(240);
image(drawingSurface, 0, 0);
}
void mouseClicked(){
DrawImage();
redraw();
}
void mouseDragged(){
DrawImage();
redraw();
}
void DrawImage()
{
int x = mouseX;
int y = mouseY;

color c = color(0, 25); // Here I apply the alpha

// 30 px wide square
drawingSurface.beginDraw();
for(int i = x-15; i < x+15; i++)
for(int j = y-15; j < y+15; j++)
drawingSurface.set(i, j, c);
drawingSurface.endDraw();
}

If you uncomment the background() call, you will get the behavior you expect.
Actually, no, because you replace each existing pixel with your translucent pixels. So you don't get the transparency effect, ie. a square over another will not have the intersection darker.
Calling point() (at least for Java2D) won't help, because I think there is no transparency on strokes. I could simulate with rect()...
Code:
void DrawImage()
{
int x = mouseX;
int y = mouseY;

color c = color(0, 25); // Here I apply the alpha
drawingSurface.noStroke();
drawingSurface.fill(c);

// 30 px wide square
drawingSurface.beginDraw();
for(int i = x-15; i < x+15; i++)
for(int j = y-15; j < y+15; j++)
drawingSurface.rect(i, j, 1, 1);
drawingSurface.endDraw();
}

drawingSurface.rect(x - 15, y - 15, 30, 30); (without loop) would be much more efficient!  Tongue
Re: Basic help with Alpha behaviour
Reply #3 - Apr 7th, 2009, 2:40pm
 
Thanks loads guys. I finally solved it by using the P2D rendering engine instead of the JAVA2D one, and there, using point instead of set for printing the actual pixel, but previously making a call to stroke, specifying color and alpha. At the same time, I removed the noLoop sentence, as well as the redraw ones, and like this, it seems to provide the functionality I was after.
Thanks a lot for everything once more!

/Fer
Page Index Toggle Pages: 1