I assure you I have spent hours trying to find a solution on the internet to this already, but I am in dire need of a fresh pair of eyes. I am using ArrayLists at the moment but have tried using a normal object array and the same problem occurred. For context I am trying to simulate an epidemic and am attempting to populate an array of the type "Person" with random positions within the boundaries of a community. The Person class at the moment is as follows:
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;
}
}
My Community class is as follows:
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();
}
}
}
The show() method for the community is called once every frame in the draw() method in my main Simulation class, which for reference sake, looks like this so far:
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();
}
The idea is to display all the people in the Persons array within the community rectangle, which is happening, it just looks like 1 person because they are all being drawn at the same position. I know this because upon debugging, I saw that while adding to the personArr in the community setup, the random positions were different, but each time I added to the array list, the entire list was populated with the exact same Person. This resulted in the whole list consisting of the last person that was created. I would appreciate it if someone knew why! I just need the list to be populated with the individual Person objects! Thank you <3
In case you want to try and run the project, here is the code for the grapher:
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++;
}
}