1. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## Theorical question

Hello,
I want to add to my JPanel something like 5-6 balls (could be a picture) that fall down from the sky randomly.. looks like snowing for example.
How would I do that?
I mean would it be an array of balls?with random x and y positions?when I thought about it generally i thought that it would be easy but when I start to code it I didn't knew how to manage that.
They can't touch each other fall on random time and positions.

Any help?
Thanks.

2. ## Re: Theorical question

would it be an array of balls?with random x and y positions?
Yes! thats exactly how that is done.

You can include all kinds of information here, this is a great project for learners - there are many programming aspects covered here.

Your most basic form of this application would involve a simple array of x,y positions which could be generated randomly if you like. You would then simply iterate through the array and draw a ball at each coordinate.

The next form of this app would be to make them bounce around. This is a great time to model things more verbosely - create a Ball class with attributes, like position, direction, color, etc... Your array would now contain a collection of balls instead of just points. You can animate the balls by incrementing their position in every loop.

Next, you could add basic collision detection by looking to see if a ball has hit a wall or not, and if so, reverse it's direction. This will give you a fun, balls bouncing all around effect.

Finally to really make a big finish, make a version which simulates gravity - start the balls at random heights and use an algorithm to make them accelerate towards the ground and rebound when they collide. You can fake this effect with a fibonacci sequence, which many simple games do rather than using real gravity formulas. Or you could just make one up.

If you really want to go balls-to-the-walls (har har), add sliders to the app that let you adjust gravity in real time, and spawn balls by clicking on the screen. You could even make the balls detect each other and bounce off each other.

I've spent a fair amount of time building this exact toy back in my early programming days. Have fun!
Last edited by quad64bit; 11-13-2012 at 05:47 PM. Reason: typos

3. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

No one?

4. Moderator
Join Date
Apr 2009
Posts
13,319
Rep Power
23

## Re: Theorical question

Originally Posted by tnrh1
No one?
?

5. ## Re: Theorical question

Originally Posted by Tolls
?
Mwah, invisible ink; I know these things ...

kind regards,

Jos

6. Senior Member
Join Date
Aug 2011
Posts
251
Rep Power
6

## Re: Theorical question

That's very wierd.
When I replayed I couldn't see quad replay and now I do.
Thank you for your replay but something is still not clear to me.
All of the ball objcets will be created in run time or before?I mean incase that I want that each ball will be splitted into 2 balls after 10 seconds how would I do that?
I dont want to be limited.

Thanks.

7. ## Re: Theorical question

You can do it any way you like. If working with dynamically sized collections (like arrayLists) you can spawn as many balls as you want at runtime. You can't actually split a ball, but you can have an event where you spawn a new ball in the same place as another ball which will mimic the effect of a ball splitting into two.

8. ## Re: Theorical question

Please go through the Forum Rules -- particularly the third paragraph.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•