Avoid creating a new anonymous Runnable each frame.

Since the Runnable that is passed to the EventQueue does never change,
it now is created in the constructor. This should avoid a lot of
overhead.
This commit is contained in:
Jan Philipp Timme 2014-11-06 17:34:20 +01:00
parent 1525c358c1
commit 9542131e4a

View File

@ -5,23 +5,33 @@ import java.awt.EventQueue;
import de.teamteamteam.spacescooter.gui.GameFrame; import de.teamteamteam.spacescooter.gui.GameFrame;
/** /**
* This thread triggers about 60 redraws per second. * This thread triggers the redrawing on the GameFrame.
*/ */
public class PaintThread extends TimedThread { public class PaintThread extends TimedThread {
private GameFrame gf; /**
* Runnable that is passed to the EventQueue for later invocation.
*/
private final Runnable paintRunnable;
/**
* Constructor. Sets the name of the Thread and creates the paintRunnable.
*/
public PaintThread(GameFrame gf) { public PaintThread(GameFrame gf) {
this.gf = gf; final GameFrame gameFrame = gf;
this.setName("PaintThread"); this.setName("PaintThread");
this.paintRunnable = new Runnable() {
public void run() {
gameFrame.draw();
}
};
} }
/**
* The work method invoked by the TimingThread.
*/
public void work() { public void work() {
//Trigger redrawing the things. Important: AWT-Context needed here! //Trigger redrawing the things. Important: AWT-Context needed here!
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(this.paintRunnable);
public void run() {
gf.draw();
}
});
} }
} }