Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling repaint from another class

 
Jón Jónsson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know if this is a good practice or not.
I am making a simple Invader game where you shoot laser beams at incoming invaders. I have made a class called LaserBeam which implements Runnable i.e each laser beam will have it's own thread.
My main class contains an ArrayList of all LaserBeam objects and contains a paintComponent method that updates whats happening onscreen.
Now that each beam is on a seperate thread I was thinking about passing a reference of the main class into LaserBeam so each beam can call MainClass.repaint() when it's position changes i.e every 40ms or so it moves 2 pixels up and this needs to be repainted.

Does anyone have a more elegant suggestion?
 
Stephan van Hulst
Bartender
Pie
Posts: 5790
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I wouldn't use any threads at all for the laser beams, and just one dedicated loop that updates your entire model state, and tells the UI to repaint(). You can use java.util.Timer to schedule updates at a fixed rate, or to keep running the updates as fast as possible.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic