I am generating 100 ovals as particles in the initialization method and then I want to move them by a certain amount(lets say x) for n number of iterations. I have written the following code snippet to do that.
private void drawNParticles(Graphics g)
{
ArrayList<Integer> list;
list = new ArrayList<Integer>(Collections.nCopies(n, 0));
for(int i=0;i<list.size();i++)
{
generateParticle(g);
}
}
private void generateParticle(Graphics g)
{
int radius = 4;
ArrayList<Integer> list= new ArrayList<Integer>();
list=positionParticles(particle_x,particle_y);
g.setColor(Color.RED);
g.fillOval(list.get(0),list.get(1), radius, radius);
}
private ArrayList<Integer> positionParticles(int x, int y)
{
int radius = 4;
ArrayList<Integer> list= new ArrayList<Integer>();
if(this.particle_counter==0)
{
x=randomInteger(2,678); // bounds of x between which the particles should be generated
y=randomInteger(2,448); // bounds of y between which the particles should be generated
x=x-(radius/2);
y=y-(radius/2);
if((x<251&&x>149)&&(y<266&&y>224))
{
x=0;
y=0;
positionParticles(x,y);
}
if((x<541&&x>499)&&(y<401&&y>299))
{
x=0;
y=0;
positionParticles(x,y);
}
this.particle_counter++;
}
else
{
setXPosition_particle(x);
setYPosition_particle(y);
}
list.add(x);
list.add(y);
return list;
}
public void setXPosition_particle(int x)
{
this.particle_x=x+5;
System.out.println("Particle_X:"+this.particle_x);
}
public void setYPosition_particle(int y)
{
particle_y=y+5;
System.out.println("Particle_Y:"+this.particle_y);
}
What I want is that each particle's position should be incremented by 5. But in the output, every particle gets the same value. I am getting a diagonal line across my JPanel. What should I do to access each instance variable separately?