This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.