views:

918

answers:

5

I'm looking to do some physics simulations and I need fast rendering in Java.

I've run into performance issues with Java2d in the past, so what are the fast alternatives? Is JOGL significantly faster than Java2d?

+2  A: 

I don't know - in the past I'd have said yes - especially if you use display lists rather than making lots of calls through the API each time the screen is displayed. But update 10 of the 1.6 JVM added accelerated Java2D graphics, so may have the advantage now. Really the only way to know for sure is to try to render typical scenes in both and measure it.

Pete Kirkham
+5  A: 

My experience with Java2D is that it can be very fast, if you follow the rules. I had an application that went from 90% CPU to less than 5% just by changing a few simple things. Using large transparant PNG's is a no no for example.

A very good resource is the Java Gaming.org forums: a lot of people, including the Sun 2D specialists hang out there and provide many examples and solutions to performance issues for 2D drawing.

See: http://www.javagaming.org/ and then the topic "Performance Tuning".

Rogier
+1  A: 

JOGL is a 3D drawing package, and very unlikely to be faster that Java2D for drawing things that are essentially 2D. If what you are drawing is essentially 3D then absolutely go to JOGL.

Obviously the answer depends on what you are trying to do. If you are animating a drawing of some physical process then I would recommend trying to optimize the process. Here are some suggestions:

  1. If you are drawing many symbols that are the same but in different places, create a bitmap for the symbol and draw it many times;
  2. Turn off antialiasing if you are drawing something that changes quickly
  3. If only one area of the screen is changing, make sure you redraw only that area;
  4. If some elements of the picture are not changing, consider creating a bitmap cache for the things that don't change.
DJClayworth
No, OpenGL is great for 2D too. Most of the time, OpenGL will be faster than Java2D.
Zifre
Java2D can use OpenGL for HW accelerated rendering since Java 1.5. The only drawback is that it is disabled by default and has to be enabled via a command-line switch "-Dsun.java2d.opengl=true".
TomA
+3  A: 

JOGL might be much faster than Java2D even if you use it for doing 2D graphics only: as Clayworth mentioned, it usually depends on what you need to do.

My guess is that for 2D physical simulations, where you have (textured or non-textured) objects rotating and translating with 2 degrees of freedom, JOGL should provide the best performance and also easily allow you to provide a zoomable interface. Here is a tutorial for OpenGL for 2D graphics (C, but easily adapted to JOGL).

JOGL will take a bit more time to learn than Java2D, but achieving good performance will most probably not require specialized optimizations as in Java2D.

Lale
+1 for using JOGL for 2D. I do this all the time at work and it really blows Java2D out of the water, especially for dense displays and alpha blending. On top of all of that you get GLSL shaders which are amazingly powerful.
basszero
A: 

Also check out Slick, which is based on LWJGL and is in some ways similar to Java2D. It uses OpenGL and is thus greatly faster.

MH114