Also meine Frage ist, ob hier das Double Buffering angewendet wurde?
Java:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import javax.swing.JPanel;
public class Fenster extends JPanel implements Runnable
{
// Variablen
public static final int BREITE = 800;
public static final int HÖHE = 400;
private static float timeSinceLastFrame;
private static int FPS = 30;
private boolean running = false;
private Thread thread;
private Image image;
private Graphics2D g;
Spieler spieler = new Spieler(BREITE, HÖHE);
// Konstruktor
public Fenster()
{
super();
setPreferredSize(new Dimension(BREITE, HÖHE));
setFocusable(true);
requestFocus();
}
public void addNotify()
{
super.addNotify();
if(thread == null)
{
thread = new Thread(this);
}
thread.start();
System.out.println("Thread gestartet");
addKeyListener(new Tastatur());
}
// Hauptspiel
@Override
public void run()
{
running = true;
long lastFrame = System.currentTimeMillis();
while(running)
{
long thisFrame = System.currentTimeMillis();
timeSinceLastFrame = (float) (thisFrame - lastFrame)/1000;
lastFrame = thisFrame;
Update();
Zeichnen();
Rendern();
try
{
Thread.sleep(1000 / FPS);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
System.exit(0);
} // Hauptspiel Ende
private void Zeichnen()
{
image = createImage(BREITE, HÖHE);
g = (Graphics2D) image.getGraphics();
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// Zeichnen
g.setColor(Color.GREEN);
g.fillRect(0, 0, BREITE, HÖHE);
spieler.Zeichnen(g);
} // Zeichnen Ende
private void Rendern()
{
Graphics g2 = this.getGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
}
private void Update()
{
if(Tastatur.Knopfgedrückt(KeyEvent.VK_ESCAPE))running = false;
spieler.Update(timeSinceLastFrame);
}
}