8

I am using Processing language to sketch a rectangle that grows in size with time. Following code is not giving any output.

void setup()
{
    size(900,900);
}
void draw()
{
    int edge=100;
    for(int i=0;i<300;i++)
    {
        delay(100);  
        edge++;      
        rect(100,100,edge,edge);
    }
}

I suspect having wrongly used delay() function.

dev
  • 2,474
  • 7
  • 29
  • 47

3 Answers3

10

Here is one such "roll your own" delay method which is good for most purposes. Just change the values passed into the delay method to alter the timing. This just outputs "start" and "end" roughly every 2 seconds for example.

void draw()
{
  System.out.println("start");
  delay(2000);
  System.out.println("end");
  delay(2000);
}

void delay(int delay)
{
  int time = millis();
  while(millis() - time <= delay);
}
cditcher
  • 358
  • 3
  • 12
  • This example works for println() but not for drawing shapes - the whole program is delayed before anything is drawn - regardless of where the delay is placed in the code. – Kokodoko Sep 17 '14 at 16:28
7

I recommend rolling your own delay system using the millis() function.

Have a look at this example.

Community
  • 1
  • 1
George Profenza
  • 50,687
  • 19
  • 144
  • 218
4

With processing, the screen does not get refreshed until the program flow reaches the end of draw()
Try the following:

 void setup()
 {
   size(900,900);
   frameRate(10);
 }
int edge = 100;
void draw()
{ 
     edge++;      
     rect(100,100,edge,edge);
}