首页 > 解决方案 > 在处理中填充对象数组 - 所有对象都相同

问题描述

我向你保证,我已经花了几个小时试图在互联网上找到解决方案,但我迫切需要一双新的眼睛。我目前正在使用 ArrayLists,但尝试使用普通对象数组并且发生了同样的问题。对于上下文,我正在尝试模拟流行病,并尝试在社区边界内填充具有随机位置的“人”类型的数组。目前的 Person 类如下:

private PVector pos;
class Person{

 public Person(float x, float y){
    pos = new PVector(x, y);
  }
  
  void show(){
    ellipse(pos.x, pos.y, 10, 10);
  }
  
  float xPos(){
    return pos.x;
  }
  
}

我的社区课程如下:

private float size;
private PVector origin;
private ArrayList<Person> personArr;

class Community{
  
  public Community(float x, float y, float size_){
    origin = new PVector(x, y);
    size = size_;
    
    personArr = new ArrayList<Person>();
  }
  
  void show(){
    
    noFill();
    rect(origin.x, origin.y, size, size);
    
    showPersons();
      
  }
  
  void setupCommunity(){
    
    for(int i = 0; i < initialPopulation; i++){
      
      float x1 = random(origin.x, origin.x + size);
      float y1 = random(origin.y, origin.y + size);
      
      Person person = new Person(x1, y1);
      personArr.add(person);
      
    }
    
  }
  
  void showPersons(){
    for(int i = 0; i < personArr.size(); i++){
      personArr.get(i).show();
    }
  }
  
}

社区的 show() 方法在我的主 Simulation 类的 draw() 方法中每帧调用一次,为了参考,到目前为止看起来像这样:

Grapher graphInfected, graphSuseptible, graphRemoved;
Community community;

float graphLength = 250;
float communitySize;
int initialPopulation = 25, population;
int populationInfected = 2, populationSusceptible = 23, populationRemoved = 0;

public void settings(){
  size(1700, 1000);
   communitySize = height * 0.8;
}

void setup(){
  population = initialPopulation;
  
  community = new Community(150, 100, communitySize);
  
  community.setupCommunity();
  
  graphInfected = new Grapher(width/2 + 240 + 200, height/2 - 230, "Infected", graphLength);
  graphSuseptible = new Grapher(width/2 + 240 + 200, height/2 + 90, "Suseptible", graphLength);
  graphRemoved = new Grapher(width/2 + 240 + 200, height/2 + 400, "Removed", graphLength);
  
}

void draw(){
  
  background(255);
  
  community.show();
  
  graphInfected.addToArray(populationInfected);
  graphSuseptible.addToArray(populationSusceptible);
  graphRemoved.addToArray(populationRemoved);
  
  graphInfected.show();
  graphSuseptible.show();
  graphRemoved.show();

}

这个想法是在社区矩形内显示 Persons 数组中的所有人,这正在发生,它看起来就像 1 个人,因为他们都被绘制在同一个位置。我知道这一点是因为在调试时,我看到在社区设置中添加 personArr 时,随机位置不同,但每次我添加到数组列表时,整个列表都填充了完全相同的 Person。这导致整个列表由创建的最后一个人组成。如果有人知道为什么,我将不胜感激!我只需要用单个 Person 对象填充列表!谢谢<3

如果您想尝试运行该项目,这里是绘图员的代码:

class Grapher {
  private IntList population;
  private int countArr = 1, dataLength = 0;
  private PVector[] linePos;
  private PVector origin;
  private String graphType = "";
  private float length;
  private String yLable = "";
  
  Grapher(int x, int y, String graphType, float length){
    
    origin = new PVector(x, y);
    population = new IntList();
    this.graphType = graphType;
    this.length = length;
    
    population.set(0, initialPopulation);
    
  }

  //Called every every frame
  void show() {

    dataLength = population.size();
    linePos = new PVector[dataLength];
    
    //background(255, 255, 255);

    int largestPop = initialPopulation;

    for (int i = 0; i < dataLength; i++) {
      if (population.get(i) > largestPop) {
        largestPop = population.get(i);
      }
    }

    //UI code
    stroke(0, 0, 0);
    line(origin.x, origin.y, origin.x + (int) length, origin.y);

    fill(0);
    textSize(15);
    text("" + largestPop, origin.x - 60, origin.y - length);
    text("" + dataLength, origin.x + length, origin.y + 25);

    fill(0);
    textSize(15);
    text("Time", (float) (origin.x  + length/2 - length/10), origin.y + 30);
    text(yLable, origin.x - 100, (float) (origin.y - length/2));

    //Calculating the graph points
    line(origin.x, origin.y, origin.x, origin.y - (int) length);

    double yInterval = length/(largestPop);
    double interval = length/dataLength;

    for (int i = 0; i < dataLength; i++) {

      float xPos = origin.x + (float) interval * i;
      float yPos = origin.y  - ((float) yInterval * (population.get(i)));

      linePos[i] = new PVector(xPos, yPos);

      //ellipse(xPos, yPos, 5, 5);
    }

    //Picking the graph colour
    if(graphType.equalsIgnoreCase("Infected")){
      stroke(255, 0, 0);
      yLable = "Infected";
    }else if(graphType.equalsIgnoreCase("Susceptible")){
      stroke(0, 0, 255);
      yLable = "Susceptible";
    }else{
      stroke(0, 0, 0);
      yLable = "Removed";
    }

    //Drawing the graph and connecting the points
    for (int i = 0; i < dataLength - 1; i++) {
      line(linePos[i].x, linePos[i].y, linePos[i + 1].x, linePos[i + 1].y);
    }
  }
  
  void addToArray(int population){
    this.population.set(countArr, population);
    countArr++;
  }
}

标签: processing

解决方案


PVector pos 放置在 Person 类之外。所以所有的 Person 对象都使用相同的 pos。

社区课程也是如此。如果您创建多个社区,可能会导致奇怪的错误。


推荐阅读