处理:在循环内绘制延迟线

时间:2014-06-24 15:20:00

标签: loops import delay processing draw

我想在外部.txt文件中显示路径,其中包含如下数据:

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

这是代码

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);
}

我希望点之间的线条被延迟绘制,经过一段时间后谷歌我想出了自定义延迟函数del(),这不会起作用,因为在循环内部不会发生任何帧。任何想法如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

试试这个:我使用noLoop来停止绘制循环并调用redraw来更新显示。 Redraw调用draw方法。同样对于你的del函数你有del(10)评论为10秒延迟。实际上是10毫秒。 del(10000)是10秒延迟。

 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);
    }