Search code examples
javagame-physics

Problems with render in a game developing in Java


Happy new year!

I am studying game developing in Java and a problem showed up in my Render method.

When i call render method with a simple code like this:

public void render() {
    Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics();
    g.setColor(Color.black);
    g.fillRect(0, 0, mainWindow.getWidth(), mainWindow.getHeight());
    onRender(g);
    g.dispose();
    bufferStrategy.show();
}

and onrender a simple cicle being drawed in red color

but my netbeans and my computer get really, really slow.

When i type top in Debian terminal i can see that my cpu usage in a "Java" application is 100% more.

and i don't know why. Can someone tell me what am i doing wrong?


Solution

  • Add a small delay between render/update cycles to give the cpu time to deal with what you're trying to do, use sometng go like Thread.sleep(40) (25fps)..assuming there's a update loop in there somewhere...

    Also take a look at BufferStrategy JavaDocs, which has a simple example of how you should use it (excluding a small delay)