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,23 +22,24 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
final GameFrame gf = new GameFrame(); final GameFrame gf = new GameFrame();
//Whatever.
new TestEntity();
new Player();
//Initialize the GameFrame properly within the AWT EventQueue //Initialize the GameFrame properly within the AWT EventQueue
EventQueue.invokeLater(new Runnable() { EventQueue.invokeLater(new Runnable() {
public void run() { public void run() {
gf.init(); gf.init();
gf.drawEntities(); //Draw nothing for the first time.
} }
}); });
//Whatever.
new TestEntity();
new Player();
//Initialize PaintThread //Initialize PaintThread
PaintThread pt = new PaintThread(gf); PaintThread pt = new PaintThread(gf);
pt.start(); pt.start();
//Initialize EntityUpdateThread //Initialize EntityUpdateThread
EntityUpdateThread ut = new EntityUpdateThread(gf); EntityUpdateThread ut = new EntityUpdateThread(gf);
ut.start(); ut.start();
} }
} }

View File

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