wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Multiple threads with graphical components Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Multiple threads with graphical components" Watch "Multiple threads with graphical components" New topic
Author

Multiple threads with graphical components

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have a JFrame, within that i have a JPanel that i would like to add a number of balls to, which will each move independently bouncing off the bounding box.
I have managed to get it working with 1 ball and a timer class, but now i am trying to get it to work with threads, so i assign 1 thread to each ball.
At the moment my structure is
BallFrame extends JFrame
BallsPanel extends JPanel
Ball.java extends component
BallRunnable implements runnable

I have added a println into the run method in BallRunnable and that prints out so i know my thread is running it is just not painting my component. Could someone tell me where i have gone wrong.

BallRunnable


Ball


and this is where i add a ball to my JPanel (from my JFrame) and also set the thread for the ball
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
I don't understand your BallRunnable class. Why does it have a paintComponent() method when it isn't a GUI component? What is supposed to call this method? Also why does the first line of the method then recursively call itself which can only lead to a stack overflow? Finally the move method changes the x, y values of the BallRunnable object and not of the Ball.

The Ball class has a draw() method which I'm guessing should be paintComponent() as paintComponent() will be called as a result of the BallRunnable object calling repaint() on the Ball.
tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
This thread can probably be removed as all the classes and structure have been completely changed, thankfully everything is now working though.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Glad to hear you have it working.
We don't remove threads just because they are resolved as the information here may help someone else in the future.
 
wood burning stoves
 
subject: Multiple threads with graphical components