*
The moose likes Swing / AWT / SWT and the fly likes multiple thread JPanel animation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "multiple thread JPanel animation " Watch "multiple thread JPanel animation " New topic
Author

multiple thread JPanel animation

Mark Nuttall-Smith
Greenhorn

Joined: Jan 11, 2006
Posts: 4
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

Joined: Jan 11, 2006
Posts: 4
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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Jan 11, 2006
Posts: 4
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
 
Consider Paul's rocket mass heater.
 
subject: multiple thread JPanel animation
 
Similar Threads
Java animation :confused:
Balls are not moving
Using Threads
Calling an applet from another class of the same package
Bouncing Ball Problem