returning a rectangle through an arraylist
i have an arraylist that creates balls at random points x and y with set height and width of 20. each ball is created every 3 seconds and then stored in the arraylist
Code :
public ArrayList<Ball> BALLS;
thats the array for storing the balls, which are made in this class
Code :
class Ball
{
int x;
int y;
int width;
int height;
public Ball(int x, int y, int width, int height)
{
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}//end ball
public void paint(Graphics g)
{
g.setColor(color[getRandomColor()]);
g.fillOval(x, y, width, height);
} //end paint
im trying to return the balls as rectangles with the getBallBounds method so i can use that method for collision detection
this is in my ball class
Code :
public Rectangle getBallBounds()
{
for(int i = 0; i < BALLS.size(); i++)
{
return new Rectangle(BALLS.get(i).x, BALLS.get(i).y, 20, 20);
}
Rectangle ballRectangle = getBallBounds();
}
whats wrong here? it says im not returning a rectangle even tho it has an x and y value with height and width
Re: returning a rectangle through an arraylist
Code java:
public Rectangle getBallBounds()
{
for(int i = 0; i < BALLS.size(); i++)//BALLS.size() might as well just be 1....
{
//....because the first time this loop runs, you hit a return statement, and exit the loop and method.
return new Rectangle(BALLS.get(i).x, BALLS.get(i).y, 20, 20);
}
//This line of code is never able to run
Rectangle ballRectangle = getBallBounds();
}
Inspect the comments and repost the code with corrections if you have more questions.
Re: returning a rectangle through an arraylist
Code :
public Rectangle getBallBounds()
{
for(int i = 0; i < BALLS.size(); i++)
{
new Rectangle(BALLS.get(i).x, BALLS.get(i).y, 20, 20);
}
return ???;
}
this makes more sense now lol. but what would i return in order to create a rectangle for each ball stored in the arraylist?
alright i made an object Rectangle randomBallRectangle = getBallBounds(); in the main game class and then changed the return to
return randomBallRectangle;
i get no errors until i run it and i get this:
java.lang.NullPointerException
at game.getBallBounds(game.java:290)
at game.<init>(game.java:38)
Re: returning a rectangle through an arraylist
Ok, lets have a looksee one step at a time.
It seems to me you want a Rectangle for EACH ball, your method can only return ONE Rectangle. You would have to return BALLS.size() Rectangles to get one for each. You could return an ArrayList of Rectangles to get one for each, however don't do that. Since returning an ArrayList of calculated data can be broken down into smaller steps, that would be the better approach. Get your method to return just one Rectangle to start. After you get that working, you can make another method to get a Rectangle for each ball by using a loop to call your method for one rectangle multiple times.
For the return in the method, if you were to try to return a Rectangle for each ball, you would create, for example, an ArrayList of Rectangles, populate the list within the loop, and finally return the ArrayList. I will show code for how this would work, but don't do this in your program. This is just so you can see how it would work in the way you were originally creating the method.
Code java:
public ArrayList<Rectangle> getBallBounds() {
ArrayList result = new ArrayList<Rectangle>();
for(int i = 0; i < BALLS.size(); i++) {
result.add(new Rectangle(BALLS.get(i).x, BALLS.get(i).y, 20, 20));
}
return result;
}
For the NullPointerException, without seeing the rest of your code I can't offer any more input than what the error message already tells you. Like what the problem is, and the line number where the problem is suspected to be.
Re: returning a rectangle through an arraylist
i have this code that creates a new ball every 3 seconds
Code :
if (timeSinceLastNewDot >= NEW_DOT_FREQ)
{
int newX = randomNumber();
int newY = randomNumber();
debugPrint("New dot created at x:" + newX + ", y:" + newY + ".");
BALLS.add(new Ball(newX, newY, 20, 20));
timeSinceLastNewDot = 0;
}
could i just run the getBallBounds method from inside the loop statement so that it gets called every time a new ball is drawn?
Re: returning a rectangle through an arraylist
Re: returning a rectangle through an arraylist
alright im back at square one and im completely confused. ill take this step by step:
1. created this method within the ball class in order to create a method that returns the dimensions of the ball
Code :
public Rectangle getBallBounds()
{
return new Rectangle(this.x, this.y, 20, 20);
}
2. In the updateGame() method, i want to run the getBallBounds() method everytime a new ball is created, but im not sure if this is the right way to do this
3. Another alternative would be creating a for loop like you suggested and storing the rectangles in an array list, but you said not to do this for some reason
Re: returning a rectangle through an arraylist
Quote:
Originally Posted by
burntcasadilla
alright im back at square one and im completely confused. ill take this step by step:
1. created this method within the ball class in order to create a method that returns the dimensions of the ball
Code :
public Rectangle getBallBounds()
{
return new Rectangle(this.x, this.y, 20, 20);
}
Looks good to me. I think the method could probably be static. Each ball would use the same process to return it's own bounds.
Quote:
Originally Posted by
burntcasadilla
2. In the updateGame() method, i want to run the getBallBounds() method everytime a new ball is created, but im not sure if this is the right way to do this
Sounds ok to me, without seeing the rest of the code.
Quote:
Originally Posted by
burntcasadilla
3. Another alternative would be creating a for loop like you suggested and storing the rectangles in an array list, but you said not to do this for some reason
1)Calling a method for the bounds of a ball when you need it, sounds like a good way to handle it. Allowing the ball handle it's own data rather than storing the same thing twice.
2)The loop is a bad idea because the ball's bounding box belongs to the ball, and should be accessed through the class methods. Storing the same thing a new ArrayList is one more ArrayList than you need to handle with the program.