• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple threads with graphical components

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic