• Post Reply Bookmark Topic Watch Topic
  • New Topic

Changing Horizontally bouncing to vertically bouncing balls  RSS feed

 
Sharon Du
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm new to java and i need help to change these horizontally bouncing balls to vertically bouncing balls.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
552
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharon,
Welcome to CodeRanch!

Can you identify what part of that code changes the position of the balls? (It's less than 10 lines). Then take a piece of paper and draw the original position at 0,0. Add the numbers in your program to have it move horizontally. Then think about what numbers you need to change to have it bounce vertically.

Post here your thoughts or where you got stuck looking at the code.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sharon,

after having answered Jeanne's questions and solved the problem, it is time to look at
another problem.

In your start() method, you start a new Thread. That's okay, but can be a potential problem.
If, in your browser, you leave the page, and later revisit that page, your start() method will be
called again, causing now TWO threads that control your applet, and so on.
The remedy is to abort your thread in the stop() method, but this is not so straightforward
to achieve.
There is a much better and easier way to deal with this, so come back later when you
have your vertical bouncing balls.

Greetz,
Piet
 
Sharon Du
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice; i managed to solve it after thinking it over a few times

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sharon,

yes, this looks like it. You might want to have both horizontal and vertical movement next,
see if you can implement that. One slight remark: maybe you should issue the 'repaint()'
command after you check for minimum and maximum y-values. That will prevent
your balls from a possible 'shock'.

Next: my reply was to point you to the use of a Swing Timer, in stead of using a thread
to do all the updating. If you have such a Timer, let's call it timer, then all you need to do
in your 'start()' method is: 'timer.start()', and in your 'stop()' method: 'timer.stop()'.

Greetz,
Piet
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!