Results 1 to 8 of 8
  1. #1
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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 04:47 PM. Reason: typos

  3. #3
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default Re: Theorical question

    No one?

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: Theorical question

    Quote Originally Posted by tnrh1 View Post
    No one?
    -Tolls looks at quad64bit's post-
    ?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Theorical question

    Quote Originally Posted by Tolls View Post
    -Tolls looks at quad64bit's post-
    ?
    Mwah, invisible ink; I know these things ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    3

    Default 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. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: Theorical question

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

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 2
    Last Post: 05-25-2012, 04:00 PM
  2. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  3. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  4. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  5. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM

Posting Permissions

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