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,41 +24,48 @@ public class Main {
* @param args Command line arguments.
*/
public static void main(String[] args) {
GraphicsSettings gs = new GraphicsSettings(); //Get settings
GameConfig.windowWidth = 800;
GameConfig.windowHeight = 650;
//Instantiate the GameFrame
final GameFrame gameFrame = new GameFrame();
//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);
gameFrame.init();
gameFrame.draw(); //Draw nothing for the first time.
}
});
//Initialize GameThread
PaintThread paintThread = new PaintThread(gameFrame);
paintThread.setHz(gs.getRefreshRate()); //This may be set depending on the system graphic settings.
paintThread.start();
//Initialize UpdateThread
UpdateThread updateThread = new UpdateThread();
updateThread.setSuperScreen(superScreen);
updateThread.setHz(100); //This shall remain constant across all systems.
updateThread.start();
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
GraphicsSettings gs = new GraphicsSettings(); //Get settings
GameConfig.windowWidth = 800;
GameConfig.windowHeight = 650;
//Instantiate the GameFrame
final GameFrame gameFrame = new GameFrame();
//Initialize SuperScreen and add to GameFrame, so we can call doPaint() on it.
final SuperScreen superScreen = new SuperScreen(null);
gameFrame.setSuperScreen(superScreen);
//Set up the LoadingScreen
superScreen.setOverlay(new LoadingScreen(superScreen));
//Start loading and everything will follow up.
Loader.load((LoadingScreen) Screen.currentScreen);
//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);
paintThread.setHz(gs.getRefreshRate()); //This may be set depending on the system graphic settings.
paintThread.start();
//Initialize UpdateThread
UpdateThread updateThread = new UpdateThread();
updateThread.setSuperScreen(superScreen);
updateThread.setHz(100); //This shall remain constant across all systems.
updateThread.start();
//Set up the LoadingScreen
superScreen.setOverlay(new LoadingScreen(superScreen));
//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