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

查看:17
本文介绍了如何在处理中使用 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 =graphics.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() 只是检索数组列表的大小(类似于数组的长度属性).所以简单来说,第一个循环从添加到列表的最后一个元素(最近的)开始,谁的索引等于列表大小 -1,并在第二个元素处停止,谁的索引是 1(因为数组/数组列表从 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);
  }
}

如果你只是想根据距离在 Drawing 对象之间画一条线,你可以不用反向循环:

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天全站免登陆