Search code examples
javaoptimizationframe-rate

How can I speed up my Java game's FPS?


I am making a Java game, and the game lags a lot when I paint out the graphics. The way I am currently painting out the graphics is to make a BufferedImage, get the Graphics2D from it, then do a whole bunch of:

g2d.drawImage(loadedImages.getImage(),x,y,null);

After I print all of the images to the BufferedImage, I paint the BufferedImage to the screen.

There are a lot of images I paint to the BufferedImage. Is there a better way to do this that would speed up the painting time? I do not know much about graphics and graphic cards. Should I use a graphics card? Should I paint directly to the screen? Should I use something entirely different than drawImage()?


Solution

  • The performance should be good if you're drawing a reasonable amount of images.

    Make sure you're not creating new BufferedImages every time you draw to the screen. For example, you might have a Resources singleton in which you manage all of your images so that you only load and unload each image once.


    If you really want more performance, you'll want to use OpenGL. See LWJGL, libgdx, or JOGL. You may also want to consider 2D graphics libraries like Slick.