如何在处理中使用ArrayList? [英] how to use ArrayList in Processing?

查看:118
本文介绍了如何在处理中使用ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

官方解释在这里: ArrayList

但是,它使用了迭代并使我感到困惑.

However, it uses iteration and confuses me.

我正在尝试制作绘图笔,但这里出了点问题:

I am trying to make a drawing pen but something is wrong here:

drawings.get(i) = drawing.get(i-1);


   ArrayList <Drawing> drawings = new ArrayList <Drawing>();

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

   void draw(){}

   void mouseDragged(){

     drawings.add(new Drawing(mouseX,mouseY));

     for(int i = drawings.size()-1;i>0;i--){
       drawings.get(i) = drawing.get(i-1);
      }

     for(int i=0;i<drawings.size;i++){
       fill(c,100);
       drawings.get(i).display();}
  }


  class Drawing{
     float x,y,r;
     color c;

   Drawing(float ax,float ay){
     x=as;
     y=ay;
     r=random(2,20);
     c=color(random(100,200),255,255);
    }  

   void display(){
     fill(c,100);
     ellipse(drawing[i],r,r);}

  }

我仍然不知道如何使用ArrayList. 有人知道吗?

I still don't know how to use ArrayList. Does anyone know?

谢谢.

推荐答案

您提到的迭代使您感到困惑.这是一个简单的for循环.如果您在理解这些内容时遇到麻烦,则与其他语句相比,它们看起来更难,但这仅仅是因为循环一次执行3件事:

You mentioned iteration confuses you. It's a simple for loop. In case you're having trouble understanding those, they look harder compared to other statements, but that's just because a loop does 3 things at once:

  1. 初始化柜台
  2. 将当前计数器值比较为一个极限(布尔表达式)
  3. 增加计数器

在您的代码中,有两个循环:

In your code, there are two loops:

 for(int i = drawings.size()-1;i>0;i--)

for(int i=0;i<drawings.size();i++)

第一个循环向后计数,因此:

The first loop counts backwards, therefore:

  1. 计数器以最大值(i = drawings.size())初始化
  2. 限制为1(i>0)
  3. 计数器递减(i--)(或者,如果愿意,则递增-1)
  1. the counter is initialized with the highest value (i = drawings.size())
  2. the limit is 1 (i>0)
  3. the counter is decremented (i--) (or incremented by -1 if you like)

drawings.size()仅检索数组列表的大小(类似于数组的length属性).因此,简单来说,第一个循环从添加到列表的最后一个元素(最新的元素)开始,谁的索引等于列表大小为-1,而在第二个元素的索引停止,索引的值为1(因为arrays/array)列表从0开始索引.

drawings.size() simply retrieves the size of the array list (similar to the length property of an array). So in simple terms, the first loop starts with the last element added to the list (the most recent), who's index is equal to the list size -1 and it stops at the 2nd element, who's index is 1 (since arrays/array lists start indexing from 0).

第二个循环更简单,因为它的计数从0到数组列表的大小,基本上是列表中所有元素的存储顺序(从最旧到最新).

The second loop is simpler, since is counts from 0 to the size of the array list, basically all the elements of the list in the order they were stored (oldest to newest).

在第一个循环中,您似乎将除第一个以外的所有元素都移了一个. 您应该这样尝试:

In the first loop, it looks like you're shifting all the elements except the first by one. You should try it like so:

  for (int i = drawings.size()-1;i>0;i--) {
    Drawing current  = drawings.get(i);//store the current drawing
    Drawing previous = drawings.get(i-1);//store the previous drawing
    current  = previous;//point the current to the previous
  }

这是修正了错误的代码清单:

And here is your code listing with the errors fixed:

ArrayList <Drawing> drawings = new ArrayList <Drawing>();
color c = color(0,0,192);

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

void draw() {
}

void mouseDragged() {

  drawings.add(new Drawing(mouseX, mouseY));

  for (int i = drawings.size()-1;i>0;i--) {
    Drawing current  = drawings.get(i);//store the current drawing
    Drawing previous = drawings.get(i-1);//store the previous drawing
    current  = previous;//point the current to the previous
    //drawings.get(i) = drawing.get(i-1);
  }

  for (int i=0;i<drawings.size();i++) {
    fill(c, 100);
    drawings.get(i).display();
  }
}


class Drawing {
  float x, y, r;
  color c;

  Drawing(float ax, float ay) {
    x=ax;
    y=ay;
    r=random(2, 20);
    c=color(random(100, 200), 255, 255);
  }  

  void display() {
    fill(c, 100);
//    ellipse(drawing[i], r, r);
    ellipse(x,y,r,r);
  }
}

更新

由于反向循环,我假设您需要存储图形列表并将其偏移,如下所示:

Because of the reverse loop I assumed you needed to store a list of drawing and offset it, like so:

ArrayList <Drawing> drawings = new ArrayList <Drawing>();

void setup() {
  size(400, 400);
  smooth();
  noStroke();
  colorMode(HSB);
}

void draw() {
  background(0,0,255);
  for (int i=0;i<drawings.size();i++){
    drawings.get(i).display();
  }
}

void mouseDragged() {

  for (int i = drawings.size()-1;i>0;i--) {
    drawings.get(i).copy(drawings.get(i-1));
  }
  drawings.add(0,new Drawing(mouseX, mouseY));

}


class Drawing {
  float x, y, r;
  color c;

  void copy(Drawing copyFrom){
    x = copyFrom.x;
    y = copyFrom.y;
    r = copyFrom.r;
    c = copyFrom.c;
  }

  Drawing(float ax, float ay) {
    x=ax;
    y=ay;
    r=random(2, 20);
    c=color(random(100, 200), 255, 255);
  }  

  void display() {
    fill(c, 100);
    ellipse(x,y,r,r);
  }
}

如果您只是想根据距离在图形对象之间画一条线,则可以做到没有反向循环:

If you simply want to draw a line between Drawing objects based on distance, you can do it without the reverse loop:

ArrayList <Drawing> drawings = new ArrayList <Drawing>();

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

void draw() {
}

void mouseDragged() {

  drawings.add(new Drawing(mouseX, mouseY));

  for (int i=0;i<drawings.size();i++) {
    Drawing curr  = drawings.get(i);
    if(i > 0){                                    //if the current index is greather than 0
      Drawing prev = drawings.get(i-1);           //we can access the previous
      if(dist(curr.x,curr.y,prev.x,prev.y) < 20) {//check the distance, if it's within a certain threshold
        line(curr.x,curr.y,prev.x,prev.y);        //draw the line
      }
    }
    curr.display();
  }
}


class Drawing {
  float x, y, r;
  color c;

  Drawing(float ax, float ay) {
    x=ax;
    y=ay;
    r=random(2, 20);
    c=color(random(100, 200), 255, 255);
  }  

  void display() {
    fill(c, 100);
    ellipse(x,y,r,r);
  }
}

实际上,由于您不清除背景,因此根本不需要列表.您所需要的只是对上一个Drawing对象的引用,因此您可以检查距离并画一条线:

In fact, since you're not clearing the background, you don't need a list at all. All you need is a reference to the previous Drawing object so you can check the distance and draw a line:

Drawing prev;//variable to store the previous drawing

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

void draw() {
}

void mouseDragged() {

  Drawing curr = new Drawing(mouseX, mouseY);//create a new drawing
  curr.display();//display it
  if(prev != null){//check if there was a previous one
    if(dist(curr.x,curr.y,prev.x,prev.y) < 20) {//if so, check if the distance is within the threshold
       line(curr.x,curr.y,prev.x,prev.y);//then simply draw a line
    }
  }
  prev = curr;

}


class Drawing {
  float x, y, r;
  color c;

  Drawing(float ax, float ay) {
    x=ax;
    y=ay;
    r=random(2, 20);
    c=color(random(100, 200), 255, 255);
  }  

  void display() {
    fill(c, 100);
    ellipse(x,y,r,r);
  }
}

HTH

这篇关于如何在处理中使用ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆