Search code examples
loopsimportdelayprocessingdraw

processing: drawing lines with delay inside loop


I want to display Paths from an external .txt file with data like this in it:

2,90,22
2,83,25
2,59,20
2,60,31
2,52,35
2,43,41
2,29,48
2,23,54
2,17,66
6,525,26
8,626,14
3,379,132
3,377,143
3,367,147
3,355,153
3,341,154
3,330,155

Here is the code

ArrayList pointList;

String[] temp;
String[] lines;

int n;
int x;
int y;
int id;


void setup(){
  lines = loadStrings("positions.txt");
  size(400,400);
  pointList = new ArrayList();

}

void draw(){
  //this is empty
}

void mouseReleased(){
  strokeWeight(9);
  importDraw();
}

void importDraw(){ 

    for(int i = 0; i < lines.length; ++i)
    {

      String[] temp = split(lines[i], ',');
      int id_ = id;
      id = int(temp[0]);
      int x_ = x;
      int y_ = y;
      x = int(temp[1]);
      y = int(temp[2]);
      PVector location = new PVector(x,y);
      if (id == id_)
      { 
      if(x_-x>0) stroke(255,0,0,10); else stroke(0,0,255,10); //red lines moving left blue lines moving right
      line(x_,y_,x,y);
      }
      n++;
      del(10); // delay of 10 secs
      println("delay done" + n); // console log after delay with number of loops that passed

    }
}

void del(int t)
{
  int time = millis();
  while(millis() - time <= t);
}

I want the lines inbetween the points to be drawn with a delay, after some time with google i came up with the custom delay-function del(), which wont work because inside the loop no frame will happen. Any ideas how to fix this?


Solution

  • Try this: I used noLoop to stop the draw looping and called redraw to update display. Redraw calls the draw method. Also for your del function you have del(10) commented as a 10 second delay. When in fact is 10 milliseconds. del(10000) is a 10 second delay.

     ArrayList pointList;
    
        String[] temp;
        String[] lines;
    
        int n;
        int x;
        int y;
        int id;
        int i; //declare i here
    
        boolean running; //use this to see if draw loop has terminated
    
        void setup(){
          lines = loadStrings("positions.txt");
          size(400,400);
          pointList = new ArrayList();
          noLoop();
          running=true;
        }
    
          void draw(){
    
           if(i < lines.length){ 
           String[] temp = split(lines[i], ',');
              int id_ = id;
              id = int(temp[0]);
              int x_ = x;
              int y_ = y;
              x = int(temp[1]);
              y = int(temp[2]);
              PVector location = new PVector(x,y);
              if (id == id_)
              { 
              if(x_-x>0) stroke(255,0,0,10); else stroke(0,0,255,10); //red lines moving left blue lines moving right
              line(x_,y_,x,y);
              }
              n++;
              println("delay done" + i); // console log after delay with number of loops that passed
              ++i;
              running=true;  //restart loop
           } 
        }
    
        void mouseReleased(){
          strokeWeight(22);
          i= 0;
          while(i < lines.length){
            if(running){
               println("..........." + i);
               running=false;  //stop the loop until draw completes
               redraw();
               //del(100); //set your delay
            }
          }
        }
    
        void del(int t)
        {
          int time = millis();
          while(millis() - time <= t);
        }