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!
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!