Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple thread JPanel animation

 
Mark Nuttall-Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,

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 ]
 
Mark Nuttall-Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

...
Thread-1
Thread-2
AWT-EventQueue-0 163 161
Thread-1
Thread-2
AWT-EventQueue-0 164 162
Thread-1
Thread-2
AWT-EventQueue-0 165 163
Thread-1
Thread-2
AWT-EventQueue-0 166 164
Thread-1
Thread-2
AWT-EventQueue-0 167 165
...

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 !
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> the problem is that only one of the bouncing balls is shown

the problem is these two lines

getContentPane().add(ball1);
getContentPane().add(ball2);

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)

 
Mark Nuttall-Smith
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic