aspose file tools*
The moose likes Game Development and the fly likes Calling repaint from another class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Calling repaint from another class" Watch "Calling repaint from another class" New topic
Author

Calling repaint from another class

Jón Jónsson
Greenhorn

Joined: Feb 20, 2009
Posts: 5
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

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
 
Consider Paul's rocket mass heater.
 
subject: Calling repaint from another class