Ads 468x60px

freak2code is the blog about latest geek news,software reviews ,trends in technology and more informative stuff......

Tuesday 14 August 2012

returning a rectangle through an arraylist


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();
        }

0 comments:

Post a Comment

Recent Posts