Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: returning a rectangle through an arraylist

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

    public ArrayList<Ball> BALLS;

    thats the array for storing the balls, which are made in this class

     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
    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


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: returning a rectangle through an arraylist

    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.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: returning a rectangle through an arraylist

            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)
    Last edited by burntcasadilla; August 13th, 2012 at 05:54 PM.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.
    	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.

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: returning a rectangle through an arraylist

    i have this code that creates a new ball every 3 seconds
     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?

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: returning a rectangle through an arraylist

    Sure you can

  7. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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
            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

  8. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: returning a rectangle through an arraylist

    Quote Originally Posted by burntcasadilla View Post
    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
            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 View Post
    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 View Post
    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.

Similar Threads

  1. [SOLVED] ArrayList.get() Returning "Incompatible Type"
    By Destro in forum Collections and Generics
    Replies: 2
    Last Post: June 15th, 2012, 06:13 PM
  2. ArrayList<Class> is returning null
    By havinFun in forum What's Wrong With My Code?
    Replies: 19
    Last Post: April 14th, 2012, 04:59 PM
  3. [SOLVED] Returning ArrayList via user input
    By IanSawyer in forum Collections and Generics
    Replies: 4
    Last Post: March 27th, 2012, 05:40 PM
  4. Rectangle questions
    By stacksofamber in forum Java Theory & Questions
    Replies: 3
    Last Post: September 5th, 2011, 12:43 PM
  5. Dropping to graphic element dragged from JList
    By tua1 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 29th, 2008, 07:22 AM