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 › question regarding saving images
Page Index Toggle Pages: 1
question regarding saving images (Read 417 times)
question regarding saving images
Mar 9th, 2008, 8:18am
 
Hi,

I have written a drawing program which saves an image of the drawing once its finished, using save(). I would like to be able to capture an image of the drawing during the drawing process, either incrementally or each time the  mouse is pressed.

If anyone could advise me how this might be acheived I'd be very grateful. I am new to coding, so apologies in advance for the inelegance of this code.




float a =0.0;
float b =0.0;
float c =50.0;
float d =50.0;
float e =1000;
float f =0;
float g = -50;
float h = 50;
float i =1000;
float j =1000;
float k =-50;
float l= -50;
float m= 0;
float n= 1000;
float o = 50;
float p = -50;
float s= 255;
float x=10;
float v=0;
float w=10;
float z=5;
float y=10;




float speed = 1;
float direction = 8;
float speed_s=1;
float direction_s=10;
float speed_v=1.0;
float direction_v=10;
float direction_w=20;
float direction_z=10;


void setup(){


 size (1000,1000);
 background(0);


}


void draw(){

 stroke (255);

 strokeWeight(1) ;



 fill(s);




 rect (a,b+z,c,d+w);
 rect (e,f+z,g,h+w);
 rect (i,j-z,k,l-w);
 rect (m,n-z,o,p-w);

 rect (a+z,b,c+w,d);
 rect (e+z,f,g+w,h);
 rect (i-z,j,k-w,l);
 rect (m-z,n,o-w,p);



  rect (a,b,c,d+z);
  rect (e,f,g,h+z);
  rect (i,j,k,l-z);
  rect (m,n,o,p-z);


 a=a+(speed*direction);
 b=b+(speed*direction);
 e=e-(speed*direction);
 f=f+(speed*direction);
 i=i-(speed*direction);
 j=j-(speed*direction);
 m=m+(speed*direction);
 n=n-(speed*direction);
 s=s-(speed_s*direction_s);
 v=v+(speed_v*direction_v);
 w=w+(speed*1*direction_w);
 z=z+(speed*3*direction_z);
 if (w<-0||w>1000){
   direction_w=(direction_w*-1);
 }
 if (z<-0||z>500){
   direction_z=(direction_z*-1);
 }


 if (v<0||v>250){
   direction_v=(direction_v*-1);
 }


 if (s<0||s>250){
   direction_s=(direction_s*-1);
 }

 if (a>500||a<0){
   direction = (direction*-1);
   c=c-x;
   d=d-x;
   g=g+x;
   h=h-x;
   k=k+x;
   l=l+x;
   o=o-x;
   p=p+x;
 }
 if (c<0){
   noLoop();
  save("fileName .tga");
 }
 
}








Re: question regarding saving images
Reply #1 - Mar 9th, 2008, 9:44am
 
Wow! nice dynamic rectangles!

Updated code, look for @@@ where I made changes,
Pressing the mouse saves a file named pic_L_0012_080309043652.png
meaning it's file #12 saved on 2008-mar-09 at 4:36:52, by pressing Left mouse button.
Of course you don't need all this in the file name, but it shows what's possible.
Code:
float a =0.0; 
float b =0.0;
float c =50.0;
float d =50.0;
float e =1000;
float f =0;
float g = -50;
float h = 50;
float i =1000;
float j =1000;
float k =-50;
float l= -50;
float m= 0;
float n= 1000;
float o = 50;
float p = -50;
float s= 255;
float x=10;
float v=0;
float w=10;
float z=5;
float y=10;

float speed = 1;
float direction = 8;
float speed_s=1;
float direction_s=10;
float speed_v=1.0;
float direction_v=10;
float direction_w=20;
float direction_z=10;

//@@@
String save_name = "pic";
String save_time;
int save_count=0;

void setup(){
 size (1000,1000);
 background(0);  
}

void draw(){

 stroke (255);

 strokeWeight(1) ;

 fill(s);

 rect (a,b+z,c,d+w);
 rect (e,f+z,g,h+w);
 rect (i,j-z,k,l-w);
 rect (m,n-z,o,p-w);

 rect (a+z,b,c+w,d);
 rect (e+z,f,g+w,h);
 rect (i-z,j,k-w,l);
 rect (m-z,n,o-w,p);

 rect (a,b,c,d+z);
 rect (e,f,g,h+z);
 rect (i,j,k,l-z);
 rect (m,n,o,p-z);

 //@@@
 //uncomment these lines for some random changes
 //if (random(1)<0.01) {print("S");s=255-s;}
 //if (random(1)<0.005) {print("s");direction_s=-direction_s;}
 //if (random(1)<0.005) {print("v");direction_v=-direction_v;}
 //if (random(1)<0.005) {print("w");direction_w=-direction_w;}
 //if (random(1)<0.005) {print("z");direction_z=-direction_z;}

 a=a+(speed*direction);
 b=b+(speed*direction);
 e=e-(speed*direction);
 f=f+(speed*direction);
 i=i-(speed*direction);
 j=j-(speed*direction);
 m=m+(speed*direction);
 n=n-(speed*direction);
 s=s-(speed_s*direction_s);
 v=v+(speed_v*direction_v);
 w=w+(speed*1*direction_w);
 z=z+(speed*3*direction_z);
 
if (w<-0||w>1000){
   direction_w=(direction_w*-1);
 }
 if (z<-0||z>500){
   direction_z=(direction_z*-1);
 }

 if (v<0||v>250){
   direction_v=(direction_v*-1);
 }

if (s<0){
direction_s=-abs(direction_s);
}
else if (s>255){
direction_s=abs(direction_s);
}

 if (a>500||a<0){
   direction = (direction*-1);
   c=c-x;
   d=d-x;
   g=g+x;
   h=h-x;
   k=k+x;
   l=l+x;
   o=o-x;
   p=p+x;
 }
 if (c<0){
   noLoop();
   save("fileName .tga");
 }

}//draw()

//@@@
void mousePressed() {
   boolean mouseRight=(mouseButton==RIGHT);
   save_time=nf(year()%1000,2)+nf(month(),2)+nf(day(),2)+nf(hour(),2)+nf(minute(),2)+nf(second(),2);
   save_count++;
   //save (save_name+"_"+save_time+".png");
   if (mouseRight) {
     save (save_name+"_R_"+nf(save_count,4)+"_"+save_time+".png");
   }
   else {
     save (save_name+"_L_"+nf(save_count,4)+"_"+save_time+".png");
   }
}//mousePressed()

Note: you have this line in your program:
 if (c<0){ noLoop(); save("fileName .tga"); }
Remove it and the drawing can continue producing nice patterns.
Re: question regarding saving images
Reply #2 - Mar 9th, 2008, 10:55am
 
hey,

thank-you very much. Your code does exactly what i want it to do. I'm still trying to understand it, but it works.

thanks again.
Re: question regarding saving images
Reply #3 - Mar 9th, 2008, 11:12am
 
try with these values:
 stroke (255,50);
 fill(s,50);
It gives nice smooth images.
Re: question regarding saving images
Reply #4 - Mar 9th, 2008, 11:24am
 
try these changes for rotating rectangles:
Code:
...
int save_count=0;

float rot_cumul=0;
float rot_del=PI/64;

void setup(){
 size (1000,1000);
 background(0);  
}

void draw(){
 translate(width/2,height/2);
 rotate(rot_cumul);
 translate(-width/2,-height/2);

 stroke (255,40);
 strokeWeight(1) ;
 fill(s,40);
...

rot_cumul+=rot_del;
}//draw()
...
Page Index Toggle Pages: 1