Use invokeAndWait() instead of invokeLater() for painting.

This commit is contained in:
Jan Philipp Timme 2014-11-12 10:47:50 +01:00
parent 15eac026a7
commit 61b80ff49e
3 changed files with 55 additions and 35 deletions

View File

@ -1,6 +1,7 @@
package de.teamteamteam.spacescooter;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import de.teamteamteam.spacescooter.screen.LoadingScreen;
import de.teamteamteam.spacescooter.screen.Screen;
@ -23,6 +24,10 @@ public class Main {
* @param args Command line arguments.
*/
public static void main(String[] args) {
//Initialize the GameFrame properly within the AWT EventQueue
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
GraphicsSettings gs = new GraphicsSettings(); //Get settings
GameConfig.windowWidth = 800;
@ -33,15 +38,11 @@ public class Main {
//Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it.
final SuperScreen superScreen = new SuperScreen(null);
//Initialize the GameFrame properly within the AWT EventQueue
EventQueue.invokeLater(new Runnable() {
public void run() {
gameFrame.setSuperScreen(superScreen);
//Initialize the gameFrame and trigger a first draw.
gameFrame.init();
gameFrame.draw(); //Draw nothing for the first time.
}
});
//Initialize GameThread
PaintThread paintThread = new PaintThread(gameFrame);
@ -60,4 +61,11 @@ public class Main {
//Start loading and everything will follow up.
Loader.load((LoadingScreen) Screen.currentScreen);
}
});
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -1,6 +1,7 @@
package de.teamteamteam.spacescooter.thread;
import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import de.teamteamteam.spacescooter.GameFrame;
@ -38,7 +39,13 @@ public class PaintThread extends TimedThread {
*/
public void work() {
//Trigger redrawing the things. Important: AWT-Context needed here!
EventQueue.invokeLater(this.paintRunnable);
try {
EventQueue.invokeAndWait(this.paintRunnable);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**

View File

@ -40,6 +40,11 @@ public abstract class TimedThread extends Thread {
this.workTime = (workDone - workStart);
long timeToWait = this.workInterval - workTime;
//in case we are already running late, just print a warning and carry on!
if(timeToWait < 0) {
System.err.println("[" + this.getName() + "] workTime exceeds workInterval!:" + this.workTime + " > " + this.workInterval);
continue;
}
long msToWait = timeToWait / 1000000;
// wait using sleep for bigger intervals