0

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?

output image

Ambidextrous
  • 882
  • 2
  • 12
  • 26

2 Answers2

2

Why don't you just create a Particule class and maintain a List<Particule> ?

Dici
  • 25,226
  • 7
  • 41
  • 82
  • Okay. So Do you mean I should create my particles in the class and add them to a list when each is created? How will I access them then? Can you please give a brief example? – Ambidextrous Aug 03 '14 at 19:59
  • 2
    @learningJava [example using a class to hold the object state](http://stackoverflow.com/a/22123304/2587435) – Paul Samsotha Aug 03 '14 at 20:01
  • I was writing a poor example but peeskillet gave you a nice one ;) That's what I was talikng about. – Dici Aug 03 '14 at 20:03
  • @Dici Got it now. Thanks!. Will try to implement now. – Ambidextrous Aug 03 '14 at 20:04
  • @learningJava Also you should actually attempt to do what others have suggested in your previous posts, to use [Shapes](http://docs.oracle.com/javase/7/docs/api/java/awt/Shape.html). See more at [2D Graphics](http://docs.oracle.com/javase/tutorial/2d/index.html) – Paul Samsotha Aug 03 '14 at 20:04
  • @peeskillet Yes, I am trying to change the code to use Shapes. Once it works with basic functionality. – Ambidextrous Aug 03 '14 at 20:07
1

Your first for loop is messing up as is the generateParticle method. You should pass the index, i, into the method, you should not create a new and completely unrelated ArrayList inside the generateParticle method but instead use the ArrayList class field, list, inside of the method.

I also recommend that you not use the paint or paintComponent method to move your particles as you do not have full control over when or if it is called. Instead separate the moving of particles and their drawing by doing the moving say in a Swing Timer, and then simply iterating through the List in the paintComponent method, drawing each point.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • How would that ArrayList class field inside of method work. Do you mean I should put all my particles x and y coordinates in that? – Ambidextrous Aug 03 '14 at 20:00