Bugfix: Exceptionfreies Zeichnen.

This commit is contained in:
Jan Philipp Timme 2014-10-21 14:02:28 +02:00
parent 061ab3c9c1
commit 0fd68d8b44
2 changed files with 31 additions and 21 deletions

View File

@ -22,17 +22,18 @@ public class Main {
public static void main(String[] args) {
final GameFrame gf = new GameFrame();
//Whatever.
new TestEntity();
new Player();
//Initialize the GameFrame properly within the AWT EventQueue
EventQueue.invokeLater(new Runnable() {
public void run() {
gf.init();
gf.drawEntities(); //Draw nothing for the first time.
}
});
//Whatever.
new TestEntity();
new Player();
//Initialize PaintThread
PaintThread pt = new PaintThread(gf);
pt.start();

View File

@ -1,6 +1,7 @@
package de.teamteamteam.spacescooter.gui;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.image.BufferStrategy;
import java.util.Iterator;
@ -44,24 +45,32 @@ public class GameFrame extends JFrame {
this.createBufferStrategy(2);
Graphics bufferedGraphics = null;
BufferStrategy bufferStrategy = this.getBufferStrategy();
if (bufferStrategy == null) {
System.out.println("Mist");
}
try {
bufferedGraphics = bufferStrategy.getDrawGraphics();
//Now we can use bufferedGraphics to actually draw stuff ...
Iterator<Entity> i = Entity.entities.iterator();
while (i.hasNext()) {
Entity e = i.next();
e.paint(bufferedGraphics);
}
} finally {
//We are done, dispose the pen and celebrate the result!
if (bufferedGraphics != null) bufferedGraphics.dispose();
}
do { //while bufferStrategy.contentsLost()
do { //bufferStrategy.contentsRestored()
try {
bufferedGraphics = bufferStrategy.getDrawGraphics();
// Now we can use bufferedGraphics to actually draw stuff
// ...
Iterator<Entity> i = Entity.entities.iterator();
while (i.hasNext()) {
Entity e = i.next();
e.paint(bufferedGraphics);
}
} catch(Exception e) {
System.out.println("Hier geht was schief");
System.out.println(e);
} finally {
// We are done, dispose the pen and celebrate the result!
if (bufferedGraphics != null)
bufferedGraphics.dispose();
}
} while (bufferStrategy.contentsRestored());
bufferStrategy.show();
} while (bufferStrategy.contentsLost());
Toolkit.getDefaultToolkit().sync();
bufferStrategy.show();
}
}