I'm trying to code the usual bouncing ball program, using one thread per bouncing ball. I have two classes, the first 'BouncingBalls.java' responsible for setting up the JFrame and adding two of the second class 'Ball' JPanel objects to the contentpane. The Ball objects 'should' be responsible for changing the x,y coords of the JPanel and repainting. Basically the problem is that only one of the bouncing balls is shown. There's some debug prints in there which show that when the program is running both threads enter the run method and start looping but the repaint is only calling paintComponent() on the first thread.
Any help, advice and ideas would be greatly appreciated, Mark
[ January 11, 2006: Message edited by: Mark Nuttall-Smith ] [ January 11, 2006: Message edited by: Mark Nuttall-Smith ]
Joined: Jan 11, 2006
Ok, I've now been messing with the code a little bit more and have added a call to get the current thread name in the paintComponent method and of course the thread I thought was current isn't.
I've also read that you can't in fact have two JPanels in a JFrame without them being layed out by .add(BorderLayout.IREALLYWANTTOPUTTWOTHINGSINUSINGXYCOORDS, ball); so now I think I've been completely barking up the wrong tree.
Surely it is possible to animate two things in one JFrame !
the default layout is borderlayout, and when you add a component, without specifying 'where', the default is CENTER. You can only add one component to a 'compartment' (CENTER), so when ball2 is added, it effectively knocks ball1 out
from what I think you're trying to do, you should be adding ball2 to ball1, then adding ball1 to the contentPane
something like this (with a couple of extra changes) (not sure the 'extras' are what you're after, but at least 2 balls display)
Joined: Jan 11, 2006
Mr. Dunn, you are a scholar and a gentleman That's definitely got me on the way again. I started to think the problem was insurmountable... now I just see the nuances, or rather don't see them! <Still and always learning> Cheers, Mark