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 › beginner question on draw() function
Page Index Toggle Pages: 1
beginner question on draw() function (Read 1570 times)
beginner question on draw() function
Sep 23rd, 2005, 8:00am
 
Say that I have a 3 step For loop inside the draw function.  The loop contains instruction to draw a rectangle, and each loop completion changes the color value.  
Correct me if I'm wrong: The program will draw only after each frame(or draw function) is completed, right?  No matter how many color changing loops there are in the draw function, the program will draw only at the end of each frame, basing the color value as the last derived value from the for loop .  So in a sense, the shape does not "flicker" 3 times each frame....but only once.  

Re: beginner question on draw() function
Reply #1 - Sep 23rd, 2005, 9:08am
 
correct

the draw function represents one frame of your program, and only the result at the end of the draw() is shown to the user..

-seltar
Re: beginner question on draw() function
Reply #2 - Sep 24th, 2005, 5:48am
 
Technically, can you have a delay function inside, say, a For loop in draw(), so it looks like the framerate is made faster?(after one For loop is finished, there is a split second pause before the next loop is done)  I don't know why one would do this, just wondering if it can be done.
Re: beginner question on draw() function
Reply #3 - Sep 24th, 2005, 10:55am
 
yes, there's a delay function..

just add delay(15); for a 15 millisecond delay after each frame.. and there's lots of reason people would do that Smiley

-seltar
Re: beginner question on draw() function
Reply #4 - Sep 25th, 2005, 8:11am
 
Thanks for the help.
Here's a practice program that I just wrote.  It is supposed to draw 3 offset red lines, but between drawing each line there is a pause.
Er, unfortunately it doesn't work.  What's wrong with it?
Code:


int firstloop = 1;
void setup(){
colorMode(HSB, 100);

}
void draw() {
for(int i=1;i<=3;i++){
stroke(200, 100, 0);
line(i*20,0,i*20,height);
delay(2000);
}
if(firstloop==1){
background(90);
firstloop=0;
}
background(90);
delay(2000);
}
Re: beginner question on draw() function
Reply #5 - Sep 25th, 2005, 11:22am
 
Is this what you're after?

Code:


int count = 0;

void setup(){
size(400, 400);
colorMode(HSB, 100);
background(90);
}

void draw() {
if (count==0) {
background(90);
}

stroke(200, 100, 0);
line(20+count*20,0,20+count*20,height);

count = (count+1)%3;

delay(500);
}

Re: beginner question on draw() function
Reply #6 - Sep 25th, 2005, 2:19pm
 
That's exactly what it should do.  Why won't this next one work, though?  Shouldn't it have the same output?

Code:

int count = 0;

void setup(){
size(400, 400);
colorMode(HSB, 100);
background(90);
}

void draw() {
if (count==0) {
background(90);
}

for(int i=0; i<=3; i++){
stroke(200, 100, 100);
line(20+count*20,0,20+count*20,height);

count = (count+1)%3;
delay(500);
}

delay(500);
}
Re: beginner question on draw() function
Reply #7 - Sep 25th, 2005, 3:29pm
 
you have a delay(500); inside the for function, which delays it 500*3 milliseconds.. so that's why it's not working the same way

-seltar
Re: beginner question on draw() function
Reply #8 - Sep 25th, 2005, 3:45pm
 
I think maybe there's a tiny bit of confusion here.

Everythign you draw inside the draw() function, is drawn to an off-screen buffer, and then once draw() has exited, the buffer you drew on, is then put on-screen.

So a delay() inside draw() just makes the draw() take longer to finish, i.e. the things you drew will not be shown for longer than if you didn't have the delay.

So line(...) delay(...) line(...) will not draw one line, wait, then draw the other, but just delay the showing of the final image with both lines drawn.
Re: beginner question on draw() function
Reply #9 - Sep 26th, 2005, 10:25pm
 
For clarification I'd like to see a simple practical use of delay.
Can't the same effect be achieved with framerate?
For example

void draw() {
...
delay(1000);
}

seems equivalent to setting framerate(1).

-mb

Re: beginner question on draw() function
Reply #10 - Sep 26th, 2005, 10:57pm
 
delay() has become less useful as the api has progressed and methods like framerate() have improved.

delay(1000); is not the same as framerate(1), because it will take longer than 0 milliseconds for your draw() function to finish. setting the framerate essentially calls something like delay() that auto-adjusts based on the number of milliseconds it took to complete draw() (and the other behind the scenes stuff that has to happen).
Re: beginner question on draw() function
Reply #11 - Sep 27th, 2005, 12:47am
 
Thank you. Makes sense to me.

-mb
Re: beginner question on draw() function
Reply #12 - Sep 27th, 2005, 1:41am
 
If you want "delay heaven", give SimPack a try:
http://www.cise.ufl.edu/~fishwick/simpackj/files/

It is likely more than you need, but delays are
a natural consequence of event scheduling. For example,
try:

http://www.cise.ufl.edu/~fishwick/simpackj/simpackproc/circles1/applet/index.html

the first circle takes a bit to come up and then it starts..

Re: beginner question on draw() function
Reply #13 - Sep 29th, 2005, 5:13am
 
Thanks for the help, everyone.
Page Index Toggle Pages: 1