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 › draw a screen, save it for background
Page Index Toggle Pages: 1
draw a screen, save it for background (Read 725 times)
draw a screen, save it for background
Apr 19th, 2007, 2:23am
 
Hello, Folks
I am trying to draw some elements to screen, and clear others. So I would like background to wipe out one animated drawing, but store another one from frame to frame, perhaps like a bouncing ball on a landscape. The bouncing ball should not create trails, but the impact locations should retain thr impact change.

How is this feat achieved?

I was trying to do background(my_image), where my_image is a copy of some of what was drawn so far, but no success yet!

Thanks, Greg
Re: draw a screen, save it for background
Reply #1 - Apr 19th, 2007, 8:08am
 
use a PGraphics, which can be used with image(PImage), to store your background.

Code:

PGraphics back;
Ball ball;

void setup () {
size(300,200);

back = createGraphics(width*4, height, P3D);
back.beginDraw();
back.background(0xFFFFFFFF);
int i = 0;
while ( i<width*4 )
{
back.stroke(0xFF000000); back.strokeWeight(1); noFill();
back.line(i,0,i,height);
i+=(int)random(5);
}
back.endDraw();

ball = new Ball(width/2,height/2);
}

void draw()
{
background(0xFFFF0000);

if ( mousePressed )
{
back.beginDraw();
back.noStroke(); back.fill(0xFFFF0000);
back.ellipse( mouseX+ball.x*3, mouseY, 10, 10 );
back.endDraw();
}
image(back,-ball.x*3,0);

ball.move();
ball.draw();
}

class Ball
{
float x,y;
float xx, yy;
Ball ( float _x, float _y )
{
x = _x; y = _y;
xx = random(0,2);
yy = random(0,2);
}
void move()
{
x += xx; y += yy;
if ( x < 0 || x > width ) xx = -xx;
if ( y < 0 || y > height ) yy = -yy;
}
void draw ()
{
ellipse(x,y,5,5);
}
}


F
Re: draw a screen, save it for background
Reply #2 - Apr 19th, 2007, 9:39am
 
Wow, great code. Thanks so much!
Greg
Page Index Toggle Pages: 1