Grüße! ich wollte spaßeshalber gern ein kleines mini java applet spiel schreiben. Aber irgendwas scheine ich schon von anfang an fundamental falsch zu machen. Denn wie kann es sein, dass ich in folgendem Minimalprogramm, framerate-drops auf bis zu 1-2 fps drinhab? Wie soll ich ruckler vermeiden, wenn er selbst hierbei ab und an aus dem nichts auf minimale 2 frames in der Sekunde abfällt? Irgendwas scheint wie gesagt katastrophal falsch zu sein. aber was? Ich danke und bitte um nachsicht...
Code:
public class main extends Applet implements Runnable
{
private Thread mthread;
private Image offscreenImage;
private Graphics offscr;
private int mywidth, myheight;
private long time_fps=0;
private float fps;
private float fpslow=9999;
public void init() {
mywidth=getSize().width;
myheight=getSize().height;
offscreenImage = createImage(mywidth, myheight);
offscr = offscreenImage.getGraphics();
}
public void start() {
mthread = new Thread(this);
time_fps=System.currentTimeMillis()-1;
mthread.start();
//Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
}
public void run()
{
long tm = System.currentTimeMillis();
while(true)
{
tm += 20;
repaint();
long timenow=System.currentTimeMillis();
fps = (float) (1000/(float)(timenow-time_fps));
if(fps<fpslow) fpslow=fps;
time_fps=timenow;
try {
Thread.sleep(Math.max(0, tm - timenow));
} catch (InterruptedException e) { ; }
}
}
public void paint (Graphics g)
{
offscr.setColor(Color.white);
offscr.fillRect(0, 0, mywidth, myheight);
offscr.setColor(Color.black);
offscr.drawString("fps: "+String.valueOf(fps),5,myheight-50);
offscr.drawString(" ### lowest: "+String.valueOf(fpslow), 5, myheight-30);
g.drawImage(offscreenImage, 0, 0, this);
}
}