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

[5681 byte] By [DeltaCodera] at [2007-10-1 21:30:30]
# 1

1. Java graphics perform poorly. This is because they do not exploit hardware / OS optimization at all. There may be 3rd party packages (I'm thinking OpenGL) that do better

2. You're thinking about this the wrong way. Every 30th of a second you should call repaint. Only the UI thread should be painting to the screen.

3.

> public void doRender() {

> BufferedImage objBuffer = new

> ew BufferedImage(this.getWidth(), this.getHeight(),

> BufferedImage.TYPE_INT_RGB);

Making a new buffer every cycle is going to slow things down a lot and chew up memory

tjacobs01a at 2007-7-13 3:25:50 > top of Java-index,Other Topics,Java Game Development...
# 2

1. Take a look at the game loop jboeing posted here it is easy to adapt and handles simple keyboard input well.

http://forum.java.sun.com/thread.jspa?forumID=406&threadID=512146

2. The book "Developing Games in Java" by David Brackeen is a pretty fast pace book if you are looking for general java game design.

This will link to the book's source code:

http://www.brackeen.com/javagamebook/

I think chapter 2 or 3 does a simple side scroller if you want to look at his version of a game loop.

ToddCorleya at 2007-7-13 3:25:51 > top of Java-index,Other Topics,Java Game Development...