Iterating through a Java collection to make these balls bounce, any hints?

Apologies if the question isn't clear but I couldn't think of another way to phrase it.

This is for a class assignment which I've been working at in BlueJ all weekend. I have to change a method (bounce) to let a user choose how many balls should be bouncing.

Other requirements are: the balls should be of different sizes and should be displayed in a row along the top of the screen BEFORE they bounce.

In order to do this I have to use a collection (ArrayList, HashMap, HashSet). So far I've used HashMap and have been able to have the user choose a number of "balls" of random sizes which place themselves in random positions in the top half of the screen.

When I try to have each ball bounce from its position at the top of the screen, ending at the right hand side I come up stuck. I can make the code draw one ball, bounce it then draw another ball, bounce it etc until the user selected number of balls has looped round.

There are two other classes, one to draw the canvas and one to draw the balls and move them. Both of which I'm not allowed to touch.

What I'm doing the wrong way is probably right in front of me but i've been staring at this code so long I thought I'd ask.

My current version of the code looks like this:

import java.awt.Color;
import java.util.HashMap;
import java.util.Random;
import java.util.Iterator;

public class BallDemo   
{
    private Canvas myCanvas;

    private HashMap<Integer, BouncingBall> ballMap;

    private int n;

    private int j;

    private BouncingBall ball;

    /**
     * Create a BallDemo object. Creates a fresh canvas and makes it visible.
     */
    public BallDemo()
    {
        myCanvas = new Canvas("Ball Demo", 600, 500);
    }

And the method I have to edit to bounce the balls:

 public void bounce(int numBalls)
    {
        ballMap = new HashMap<Integer, BouncingBall>();
        int ground = 400;   // position of the ground line
        Random randomD1 = new Random();
        Random xpos = new Random();


        myCanvas.setVisible(true);

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

        // add balls to HashMap
           for(n = 0; n < numBalls; n++) {
         ballMap.put(numBalls, (ball = new BouncingBall(xpos.nextInt(300), 50, randomD1.nextInt(200), Color.BLUE, ground, myCanvas)));
         //
            for(j= 0; j < ballMap.size(); j++) {
                ball.draw();
                boolean finished =  false;
                   while(!finished) {
                     myCanvas.wait(50);           // small delay
                     ball.move(); // bounce the ball

                        // stop once ball has travelled a certain distance on x axis
                          if(ball.getXPosition() >= 550) {
                                    finished = true;
                                }



                        }
            }






        }
       }
}

Am I even on the right lines using a HashMap? The combination of keys, values seemed the best way to go. I think I need to somehow iterate through the items placed in the collection to make them bounce using the move() method. But first I need the balls to stay in a row at the top of the screen, no matter how many the user defines.

I'm new to programming and I'm just coming up stumped.

Thanks for any help!


@16dots is partly right, except ballMap.put(numBalls, ball); will over write the same value in the hash map each time, as numBalls does not change...

The key should be unique.

It should read...

for (int n; n < numBalls; n++) {
    BouncingBall ball = new BouncingBall(xpos.nextInt(300), 50,
            randomD1.
            nextInt(200), Color.BLUE, ground, myCanvas);
    ballMap.put(n, ball);
}

boolean finished = false;
while (!finished) {
    finished = true;
    for (int j = 0; j < ballMap.size(); j++) {
        BouncingBall selectedBall = ballMap.get(j);
        selectedBall.draw();
        // Only move the ball if it hasn't finished...
        if (selectedBall.getXPosition() < 550) {
            selectedBall.move(); // bounce the ball

            // stop once ball has travelled a certain distance on x axis
            if (selectedBall.getXPosition() < 550) {
                finished = false;
            }
        }
    }
    myCanvas.wait(50);           // small delay
}
链接地址: http://www.djcxy.com/p/11392.html

上一篇: 为什么一些Google字体以斜体显示不正确

下一篇: 通过Java集合迭代来使这些球弹跳,任何提示?