Make these balls bounce through Java collection iteration, any tips?
If the problem is not clear, but I can't think of another way to express it, I apologize
This is my homework for working in BlueJ at the weekend I have to change a method (bounce) to let the user choose the number of balls to bounce
Other requirements are that balls should be of different sizes and should be displayed continuously along the top of the screen before they bounce
To do this, I have to use a collection (ArrayList, HashMap, HashSet) So far, I have used HashMap, and can let users select some random size "balls", which place themselves in random positions in the upper half of the screen
When I try to bounce each ball from the top of the screen and end on the right, I get stuck I can let the code draw a ball, bounce it back, then draw another ball, bounce it back, etc. until the user selects the number of balls to cycle
There are two other classes, one for drawing canvases and the other for drawing balls and moving them I'm not allowed to touch these two
The way I'm doing something wrong may be right in front of me, but I've been staring at this code, and I think I'll ask
My current code version is as follows:
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); }
I have to edit the method to bounce the ball:
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,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; } } } } } }
Do I use HashMap on the right boundaries? The combination of keys and values seems to be the best method I think I need to iterate over the items placed in the collection in some way so that they bounce back using the move () method But first, I need to keep the ball at the top of the screen no matter how many users define
I'm new to programming. I'm just stumped
Thanks for your help!
Solution
@The 16dots section is correct except ballmap put(numBalls,ball); The same value will be rewritten in the hash map every time, because numballs will not change
The key should be the only one
It should read
for (int n; n < numBalls; n++) { BouncingBall ball = new BouncingBall(xpos.nextInt(300),randomD1. nextInt(200),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 }