Beginner question: ArrayList can't seem to get it right! Pls help

Posted by elementz on Stack Overflow See other posts from Stack Overflow or by elementz
Published on 2010-06-08T19:53:44Z Indexed on 2010/06/08 20:02 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

I have been staring at this code all day now, but just can't get it right.

ATM I am just pushing codeblocks around without being able to concentrate anymore, with the due time being within almost an hour... So you guys are my last resort here.

I am supposed to create a few random balls on a canvas, those balls being stored within an ArrayList (I hope an ArrayList is suitable here: the alternative options to choose from were HashSet and HashMap). Now, whatever I do, I get the differently colored balls at the top of my canvas, but they just get stuck there and refuse to move at all. Apart from that I now get a ConcurrentModificationException, when running the code:

java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at BallDemo.bounce(BallDemo.java:109)

Reading up on that exception, I found out that one can make sure ArrayList is accessed in a threadsafe manner by somehow synchronizing access. But since I have remember fellow students doing without synchronizing, my guess is, that it would actually be the wrong path to go.

Maybe you guys could help me get this to work, I at least need those stupid balls to move ;-)

   /**
 * Simulate random bouncing balls
 */
public void bounce(int count)
{
    int ground = 400;   // position of the ground line
    System.out.println(count);

    myCanvas.setVisible(true);

    // draw the ground
    myCanvas.drawLine(50, ground, 550, ground);


    // Create an ArrayList of type BouncingBalls
    ArrayList<BouncingBall>balls =  new ArrayList<BouncingBall>();

    for (int i = 0; i < count; i++){
        Random numGen = new Random(); 
        // Creating a random color. 
        Color col = new Color(numGen.nextInt(256), numGen.nextInt(256), numGen.nextInt(256));
        // creating a random x-coordinate for the balls
        int ballXpos = numGen.nextInt(550);


        BouncingBall bBall = new BouncingBall(ballXpos, 80, 20, col, ground, myCanvas);

        // adding balls to the ArrayList
        balls.add(bBall);
        bBall.draw();
        boolean finished = false;
    }

        for (BouncingBall bBall : balls){

            bBall.move();

    }
}

This would be the original unmodified method we got from our teacher, which only creates two balls:

    /**
 * Simulate two bouncing balls
 */
public void bounce()
{
    int ground = 400;   // position of the ground line

    myCanvas.setVisible(true);

    myCanvas.drawLine(50, ground, 550, ground);
    // draw the ground

    // crate and show the balls
    BouncingBall ball = new BouncingBall(50, 50, 16, Color.blue, ground, myCanvas);
    ball.draw();
    BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
    ball2.draw();

    // make them bounce
    boolean finished =  false;
    while(!finished) {
        myCanvas.wait(50);           // small delay
        ball.move();
        ball2.move();
        // stop once ball has travelled a certain distance on x axis
        if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
            finished = true;
        }
    }
    ball.erase();
    ball2.erase();
}

}

© Stack Overflow or respective owner

Related posts about java

Related posts about beginner