Game Loop - FPS Low
I am averaging only 12 FPS. There is no state update or rendering. What is wrong, what can be improved? (Using J2SE 1.3.1)
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.lang.*;
import javax.swing.*;
import javax.swing.event.*;
publicclass MyGameFrameextends JFrameimplements Runnable, WindowListener{
privatefinalstatic Rectangle BOUNDS =new Rectangle(50, 50, 800, 600);
privateboolean bPause;
privateboolean bStop;
public MyGameFrame(){
super("MyFrame");
this.setBounds(BOUNDS);
this.addWindowListener(this);
this.show();
boolean bPause =false;
boolean bStop =false;
new Thread(this).start();
}
publicvoid run(){
int iRenderCount = 1;
long lTime1 = System.currentTimeMillis();
while (bStop ==false){
while (bPause ==false){
System.out.println("Rendering: " + iRenderCount++);
doRender();
long lTime2 = System.currentTimeMillis();
if ((lTime2 - lTime1) > 1000){
lTime1 = lTime2;
System.out.println("FPS: " + iRenderCount);
iRenderCount = 1;
}
}
}
}
publicvoid doRender(){
BufferedImage objBuffer =new BufferedImage(this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D objGraphics = (Graphics2D)objBuffer.getGraphics();
objGraphics.setColor(Color.white);
objGraphics.fillRect(0, 0, this.getWidth(), this.getHeight());
this.getGraphics().drawImage(objBuffer, 0, 0,null);
}
publicvoid windowActivated(WindowEvent argEvent){
bPause =false;
bStop =false;
}
publicvoid windowClosed(WindowEvent argEvent){
bPause =true;
bStop =true;
this.hide();
this.dispose();
System.exit(0);
}
publicvoid windowClosing(WindowEvent argEvent){
bPause =true;
bStop =true;
this.hide();
this.dispose();
System.exit(0);
}
publicvoid windowDeactivated(WindowEvent argEvent){
bPause =true;
}
publicvoid windowDeiconified(WindowEvent argEvent){
bPause =false;
}
publicvoid windowIconified(WindowEvent argEvent){
bPause =true;
}
publicvoid windowOpened(WindowEvent argEvent){
bPause =false;
}
publicstaticvoid main(String [] argCommand){
MyGameFrame objFrame =new MyGameFrame();
}
}
DeltaCoder

