3

I'm doing a gameengine including a physics and graphics engine. Right now my graphics engine is set to 60 fps, and sometime i notice slow updates on the screen. I have a JFrame which i add a JPanel to which moves around, meaning the graphic engine does frame.repaint() every loop. I'm starting to believe that this repainting on the frame does the small delay on screen.

The delay is such that the JPanel moves in a straight line, and suddenly it can jump a bit further than usual.

Is it better to put a JPanel "gameField" on the JFrame and then add a JPanel "player" to the gameField and repaint the gameField instead? Or is it basically the same thing as repainting the JFrame? I just heard that the JFrame is supposed to be a heavy component.

I've tried setting higher fps, which changes nothing.

Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71
  • 1
    Have you looked at writing to a buffer then writing that to the screen? – dann.dev Feb 29 '12 at 21:52
  • Would you care to ellaborate with an example? :) I'm not that in to swing – Lucas Arrefelt Feb 29 '12 at 22:02
  • 2
    Please edit your question to include an [sscce](http://sscce.org/) that exhibits the problem you describe. [`AnimationTest`](http://stackoverflow.com/a/3256941/230513) may be a useful starting point. – trashgod Feb 29 '12 at 22:06
  • 1
    You're making a game engine without knowing what a buffer is!? How are you making this game engine? Are you drawing images and shapes or using Swing components? If your using a JPanel, make sure to set double buffering on either in the constructor or by using setDoubleBuffered(true). –  Feb 29 '12 at 22:29
  • It's a work in progress, nothing fancy since i'm bad at swing. Im using a JPanel which i draw a image on by overriding its paintComponent and using g.drawImage(image, posX, posY, null); – Lucas Arrefelt Feb 29 '12 at 22:40

1 Answers1

3

Have a look at this page here, it's a lot of reading but should contain most of what you need.

EDIT: This page here is a bit more succinct

This stack overflow post here deal's with double buffering.

The best advice i can give you though, is find a copy of 'Killer game programming in Java'. You can get old ebook versions for free off the net easily. A lot of the stuff is a bit dated, but the first few chapters, which deal with making a game loop and writing to a buffer are still very much relevant and will help you a great deal in the long run!

Community
  • 1
  • 1
dann.dev
  • 2,406
  • 3
  • 20
  • 32