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 & HelpSyntax Questions › Fade ellipse trail
Page Index Toggle Pages: 1
Fade ellipse trail (Read 2296 times)
Fade ellipse trail
Jan 30th, 2010, 8:14pm
 
Hey guys I am new to processing. I was wondering if someone could help me take this code and make it so the elipse trail left behind it fades away, while the rectangles keep piling up?
It would help alot,
Thanks


float val=0;

void setup(){
size(800,600);           // Size of Background/panel
background(0);   //Color of Background, Black
frameRate(30);    //Frame rate set at 30
}



float angle;
float cosine;

void draw(){          //Value at 10
 val+= 10;
stroke(196,5,23);     //Stroke Color of the ellipse and rectangle/red
noFill();               // No fill color for the ellipse
println(mouseX);       // Print in the ellipse at mouseX position


translate(mouseX,mouseY);        //Translate the center of ellipse around mouseX, mouseY
ellipseMode(CENTER);
rotate(val);          //Rotate ellipse around the rising value of 10
ellipse(0,0,100,200);   //size and position of ellipse

 if(second()%2 == 0){
   
 
 cosine = cos(angle);
 
 translate(width/2, height/2);   //Translating the the width and height, distance the rect. spins away from ellipse.
 rotate(cosine);
 rectMode(CENTER);
 rect(0, 0, 115, 115);     //Rect. size and position
}
}

Re: Fade ellipse trail
Reply #1 - Jan 30th, 2010, 11:41pm
 
I had to change the way rectangles are drawn a bit, I'm not sure if this is the way you intended, but the code below demonstrates the general idea.

1) Fade whatever is being drawn:
Code:

 noStroke();
 fill(0, 0, 0, 10);//Change the alpha value to fade faster/slower
 rect(0, 0, width, height);


2) You don't want rectangles to fade, so draw them in a separate buffer. See code below.



Code:


PGraphics buffer;
float val=0;

void setup(){
 size(800,600);           // Size of Background/panel
 background(0);   //Color of Background, Black
 frameRate(30);    //Frame rate set at 30
 
 buffer = createGraphics(width, height, P2D);  
}

float angle;
float cosine;

void draw()
{          //Value at 10

 //Fade everything which is drawn
 noStroke();
 fill(0, 0, 0, 10);
 rect(0, 0, width, height);
 
 //Draw ellipses
 val+= 10;
 stroke(196,5,23);     //Stroke Color of the ellipse and rectangle/red
 noFill();               // No fill color for the ellipse
 println(mouseX);       // Print in the ellipse at mouseX position

 pushMatrix();
 translate(mouseX,mouseY);        //Translate the center of ellipse around mouseX, mouseY
 ellipseMode(CENTER);
 rotate(val);          //Rotate ellipse around the rising value of 10
 ellipse(0,0,100,200);   //size and position of ellipse
 popMatrix();

 //Draw rectangles in a buffer so they don't get faded
 if(second()%2 == 0)
 {
   cosine = cos(val);//You used angle here, but angle is not initialized anywhere...
   
   buffer.beginDraw();
   buffer.stroke(196,5,23);
   buffer.noFill();
   buffer.translate(mouseX, mouseY);
   buffer.rotate(cosine);  
   buffer.translate(width/2, height/2);
   //Translating the the width and height, distance the rect. spins away from ellipse.
   buffer.rectMode(CENTER);
   buffer.rect(0, 0, 115, 115);     //Rect. size and position
   buffer.endDraw();
 }

 image(buffer, 0, 0);
}

Re: Fade ellipse trail
Reply #2 - Mar 19th, 2010, 8:45am
 
Hi, sorry to bring this thread back up, but I thought it was better than starting a whole new thread on the same topic.

I'm having trouble when using the JAVA2D renderer in createGraphics(). I get a null pointer to my "image(buffer, 0, 0)" line.
I have tried changing my createGraphics() final argument to the P2D renderer, but I cant seem to get the resultant image to smooth() when displayed to the screen.

Any idea either how to either get the image to smooth with P2D, or how to fix it when using the JAVA2D renderer?

Many thanks.

Also, is there a way of clearing what has been drawn previously, when a new drawing starts? Without just using background(), as I have other shapes on the screen that must remain there.
Re: Fade ellipse trail
Reply #3 - Mar 19th, 2010, 9:37am
 
I suppose you meant with JR's code above?
I can reproduce the issue, which is rather puzzling:
Quote:
Exception in thread "Animation Thread" java.lang.NullPointerException
     at java.lang.System.arraycopy(Native Method)
     at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.
java:412)
     at processing.core.PGraphicsJava2D.updatePixels(Unknown Source)
     at processing.core.PGraphicsJava2D.imageImpl(Unknown Source)
     at processing.core.PGraphics.image(Unknown Source)
     at processing.core.PApplet.image(Unknown Source)
     at sketch_mar19a.draw(sketch_mar19a.java:74)
     at processing.core.PApplet.handleDraw(Unknown Source)
     at processing.core.PApplet.run(Unknown Source)
     at java.lang.Thread.run(Thread.java:619)

Same error with more information in 1.0.5:
Quote:
Exception in thread "Animation Thread" java.lang.NullPointerException
     at java.lang.System.arraycopy(Native Method)
     at sun.awt.image.IntegerInterleavedRaster.setDataElements(IntegerInterleavedRaster.
java:412)
     at processing.core.PGraphicsJava2D.updatePixels(PGraphicsJava2D.java:1663)
     at processing.core.PGraphicsJava2D.imageImpl(PGraphicsJava2D.java:794)
     at processing.core.PGraphics.image(PGraphics.java:2182)
     at processing.core.PApplet.image(PApplet.java:7208)
     at sketch_mar19a.draw(sketch_mar19a.java:71)
     at processing.core.PApplet.handleDraw(PApplet.java:1426)
     at processing.core.PApplet.run(PApplet.java:1328)
     at java.lang.Thread.run(Thread.java:619)

What is strange is that sometime the sketch runs without problems! (Windows XP Pro SP3, Java 1.6.0_18)
Perhaps a thread issue?
Re: Fade ellipse trail
Reply #4 - Mar 19th, 2010, 6:09pm
 
Hmmm...runs fine for me on OS X...

Maybe just for troubleshooting try to set the buffer with values instead of  width and height...
Re: Fade ellipse trail
Reply #5 - Mar 20th, 2010, 4:15am
 
I'm on OS X too, but I'm getting the NullPointerException problem.
Substituting in values doesn't seem to help either.
Cheers.
Re: Fade ellipse trail
Reply #6 - Mar 20th, 2010, 9:59am
 
This is really frustrating, as I can use the JAVA2D renderer in other sketches when writing to an off screen buffer, but for some reason I get the error when using a similar layout to the one above.

And with the smoothing imperfect for the P2D and P3D renderers I'm getting bad looking results.

Does anyone have a solution? I have even tried using an image file instead of the ellipse(), but I cant seem to retain transparency within the buffer. Any ideas?

Thanks.
Page Index Toggle Pages: 1