Java Graphics Performance

I programmed a simple game and wanted to display it using the Java Graphics + Swing API. Of course, it was a little slower, so I measured how long it took to repaint, which was about 32 ms. Then I read about Java accelerated graphics and used the method described here: Space Invaders

However, it is somehow even slower. Now it takes about 98 ms to redraw. Why is this?

Note that I know libraries like LWGL and JOGL, but I don't want to use the full-blown OpenGL shell for such a graphically simple game.

+6
java graphics
source share
10 answers
  • Read it about timers
  • Always use a sleeping thread in the background to slow down the system interruption to get much more accurate timers!
  • You can see my code in here
  • The little game I created can be found here.

    long renderStart = System.nanoTime() // render stuff here long renderTime = (System.nanoTime() - renderStart) / 1000000; 
+6
source share

Swing will be slow for the game. Best of all, it's probably an SDL shell like jsdl or sdljava . SDL is fairly lightweight and is supported on many platforms.

The good thing: well-implemented wrappers of 2d graphics libraries implement the Graphics interface, so you can try a few and see which one works best for your application without changing your code.

+6
source share

A few tips to improve performance:

  • Clip and just draw the area that has changed.
  • Do not scale anything when drawing
  • Use the correct buffer strategy
  • Use fullscreen exclusive mode if necessary.

Swing is not inherently slow. Most of the confusion about Swing comes from people who don’t know or do not understand Dispatch Thread (and yes, it’s a lot of effort to figure it out).

As for your time, are you just calling g.drawImage between the time settings? If so, what size are you redrawing, and are you doing some kind of scaling with this call?

EDIT: Forgot to mention Pulp Core - a very good gaming environment for Java.

+6
source share

I am not an expert, but Java2D supports different rendering pipelines. On my machine, switch to OpenGL by setting the system property

sun.java2d.opengl = true

led to a significant increase in rendering speed.

+3
source share

You can see processing .

+2
source share

I agree with Nicholas because the graphics / interactivity processing is great

+1
source share

Try JGame . This is a simple 2D game engine that uses GL when available, and can create games that work on anything from a mobile phone to a desktop system.

+1
source share

First of all, you should check and see how much time is spent on your own code, and how much time is spent on drawing a frame. You may have a bug or crash, which makes it run slower.

You can get much better performance than 98 ms to draw your screen. These game libraries are likely to use the same graphical calls as you. But which containers you use can have a big impact on how quickly things get done. I remember a couple of months ago, working on graphical code with double buffering, and how the backup buffer was created made a huge difference in performance.

In particular, make sure that you only create a background image once, or at least only when resizing the canvas. In my code, I do this:

  //create a graphics backplane if(backplane == null || !backplaneSize.equals(this.getSize())){ backplane = this.createImage((int)this.getSize().getWidth(), (int)this.getSize().getHeight()); backplaneSize = this.getSize(); } 

Thus, the back plane is created only if it is new, or if the size of the component changes. This has a huge impact on speed.

I am doing graphical programming in java with JDK 1.0. In fact, I never heard of this BufferStrategy thing. Heh.

I have never had problems getting quality frames with basic Java graphics calls. Another thing you need to pay attention to is to make sure that Swing is not trying to clear the background of your component from you. This can be another source of slowdown.

+1
source share

Java2D performance is a bit of a dark art. It can vary between platforms, but it can make it work well.

Sometimes doing seemingly harmless things can lead to significantly lower performance. You want to make sure that you are using accelerated images as much as possible .

The type of image may also be significant. I don’t know the details, but I know that my programs were much faster using type 1 (INT RGB) images than other types such as type 5 (3BYTE BGR) due to conversion problems.

+1
source share

I can’t say exactly what you are doing. Your question is about Swing, but you mention Canvas and Bufferstrategy. In Swing, you would use a JPanel that is automatically duplicated, so you don’t have to worry about that.

I tested a simple animation with 1000 moving balls. The timer should have been triggered every 100 ms. When the Timer shot, 1000 balls were created, where a new BufferedImage was randomly moved to a new position, and the panel displaying the image was repainted. The time difference between repainting was 110 ms, implying that to create a new BufferedImage it took only 10 ms to make a picture. The picture was made in full screen, so there were no clippings.

This post shows sample code that I tested.

+1
source share

All Articles