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) { 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();

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"); do { //while bufferStrategy.contentsLost()
} do { //bufferStrategy.contentsRestored()
try { try {
bufferedGraphics = bufferStrategy.getDrawGraphics(); bufferedGraphics = bufferStrategy.getDrawGraphics();
//Now we can use bufferedGraphics to actually draw stuff ... // Now we can use bufferedGraphics to actually draw stuff
// ...
Iterator<Entity> i = Entity.entities.iterator(); Iterator<Entity> i = Entity.entities.iterator();
while (i.hasNext()) { while (i.hasNext()) {
Entity e = i.next(); Entity e = i.next();
e.paint(bufferedGraphics); e.paint(bufferedGraphics);
} }
} catch(Exception e) {
System.out.println("Hier geht was schief");
System.out.println(e);
} finally { } finally {
//We are done, dispose the pen and celebrate the result! // We are done, dispose the pen and celebrate the result!
if (bufferedGraphics != null) bufferedGraphics.dispose(); if (bufferedGraphics != null)
bufferedGraphics.dispose();
} }
} while (bufferStrategy.contentsRestored());
bufferStrategy.show(); bufferStrategy.show();
} while (bufferStrategy.contentsLost());
Toolkit.getDefaultToolkit().sync();
} }
} }