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; package de.teamteamteam.spacescooter;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import de.teamteamteam.spacescooter.screen.LoadingScreen; import de.teamteamteam.spacescooter.screen.LoadingScreen;
import de.teamteamteam.spacescooter.screen.Screen; import de.teamteamteam.spacescooter.screen.Screen;
@ -23,6 +24,10 @@ public class Main {
* @param args Command line arguments. * @param args Command line arguments.
*/ */
public static void main(String[] args) { 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 GraphicsSettings gs = new GraphicsSettings(); //Get settings
GameConfig.windowWidth = 800; GameConfig.windowWidth = 800;
@ -33,15 +38,11 @@ public class Main {
//Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it. //Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it.
final SuperScreen superScreen = new SuperScreen(null); final SuperScreen superScreen = new SuperScreen(null);
//Initialize the GameFrame properly within the AWT EventQueue
EventQueue.invokeLater(new Runnable() {
public void run() {
gameFrame.setSuperScreen(superScreen); gameFrame.setSuperScreen(superScreen);
//Initialize the gameFrame and trigger a first draw.
gameFrame.init(); gameFrame.init();
gameFrame.draw(); //Draw nothing for the first time. gameFrame.draw(); //Draw nothing for the first time.
}
});
//Initialize GameThread //Initialize GameThread
PaintThread paintThread = new PaintThread(gameFrame); PaintThread paintThread = new PaintThread(gameFrame);
@ -60,4 +61,11 @@ public class Main {
//Start loading and everything will follow up. //Start loading and everything will follow up.
Loader.load((LoadingScreen) Screen.currentScreen); 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; package de.teamteamteam.spacescooter.thread;
import java.awt.EventQueue; import java.awt.EventQueue;
import java.lang.reflect.InvocationTargetException;
import de.teamteamteam.spacescooter.GameFrame; import de.teamteamteam.spacescooter.GameFrame;
@ -38,7 +39,13 @@ public class PaintThread extends TimedThread {
*/ */
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(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); this.workTime = (workDone - workStart);
long timeToWait = this.workInterval - workTime; 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; long msToWait = timeToWait / 1000000;
// wait using sleep for bigger intervals // wait using sleep for bigger intervals