Java 2D - Resources

Note that a much more complete FAQ can be found at:

http://java.sun.com/products/java-media/2D/reference/faqs/index.html

Frequently Asked Questions:

Q: What are those properties that speed up Java 2D’s rendering performance?

A: Check out this document for a full list: http://java.sun.com/j2se/1.5.0/docs/guide/2d/flags.html

Q: What’s the difference between BufferedImage and VolatileImage and when do I use a particular image format for displaying my sprite, jpg, png, etc?

A: http://weblogs.java.net/blog/chet/archive/2004/08/toolkitbuffered.html
or any of Chet’s other excellent blogs on 2D images
http://weblogs.java.net/blog/chet/

Q: How do I know if my Java2D images are actually being accelerated in hardware?

A: Enable JVM profiling by setting java -Dsun.java2d.trace=count in the VM arguments before running your app. Then, quit your application and look through the generated log file for D3DBlitLoops.


Q: What oses support full screen java?

A: Windows 2000 and XP are known to support full screen correctly. Other systems are still not fully catered for.

UPDATE: MacOSX supports full screen also.


Q: How do I access the pixels of a BufferedImage to do software rendering?

A:


BufferedImage tex = //get it from somewhere 
byte data[] = (byte[]) tex.getRaster().getDataElements(0, 0, tex.getWidth(), tex.getHeight(), null);  

or to get a reference to the actual buffer:


byte[] data=((DataBufferByte)tex.getRaster().getDataBuffer()).getData() 


Q: How do I access the pixels of an Image to do software rendering? (java 1.1 - optional as we don’t want to perpetuate old stuff)

A:


Image im = //get the image 
im.getSource().startProduction(ic); 

ic is an object that implements the java.awt.image.ImageConsumer interface. the method setPixels(…) will be called with the data that the image contains.


Q: How to load images and preserve tranparency?

A: If the built in loaders are used, then transparency will automatically be preserved. If you are manually loading the data, then putting it into an image you need to do some work.


//BufferedImage im = new BufferedImage(width, height, type) 
BufferedImage im = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB); 

This sets up the BufferedImage to be able to accept alpha values. Then one of three methods can be called to insert pixel values:


setData(Raster r) 
setRGB(int x, int y, int rgb)  
setRGB(int startX, int startY, int w, int h, int[] rgbArray, int offset, int scansize) 


Q: What’s accelerated by opengl pipeline in 5.0

A: Check out the Chris’ blog:
http://weblogs.java.net/blog/campbell/archive/2004/11/behind_the_grap.html


Q:
How do i enable profiling like java -Dsun.java2d.trace=count when i’m running a Applet?

A:
Either set it in the applet’s ControlPanel, or set an env. variable _JAVA_OPTIONS="-Dsun.java2d.trace=log".

You can also redirect the output to a file.
usage: -Dsun.java2d.trace=[log[,timestamp]],[count],[out:],[help],[verbose]

Q: How do I profile an Applet since I can’t set the appropriate properties?

A: Use AppletViewer that comes with the SDK to test and profile applets.


Q: I’m trying to blit alot of sprites to the screen and I just can’t get the performance I want out of Java2D. Are there any other options?

A: Yes. JOGL/LWJGL provide bindings to OpenGL which is more complicated but gives a finer level of control and in most cases gives better performance. You might also consider GTGE which gives you an abstraction above Java2D/OpenGL.


If you can think of extra common questions for this list post the question and answer below. I’ll tidy it up as time goes on. In addition, if you think an answer here is wrong also let us know.

[Question/Answer Contributors: immudium, CaptainJester, K.I.L.E.R, nonnus29,trembovetski, trycoon, Demonpants, blahblahblahh, Tusaki]

Q) how do obtain real 2D performance for a game in java?

A) learn opengl (jogl / lwjgl)

(ok ok… but still… it is an often asked question, right? ;D)

Its not a fair answer tho is it, given that 1.5 Java2D is OpenGL will the performance be ways better?

Kev

I’m not sure. altough personally I’ve had better performance using the opengl api’s directly. Also, I feel (again, this is subjective) that I have more control over what is happening. I mean, does java2d utilize all the extensions which opengl has to offer? in an intelligent way? I have a feeling that java2d controls too many aspects… “leave it to java2d so it will work on every device” … to be honest, I don’t really care about that. I want it to work on the best way for as many people as possible. which usually means windows with a mediocre graphics card.

[DevilsAdvocate] Windows with an average to poor graphics card - best option is normally DirectX or DirectDraw (depending on how poor). Java2D works through DirectDraw very nicely.

I see where you’re coming from but I think its extremely open to debate (which I’m more than happy to see :))

Kev

Ok, for a simple test case, 1000 alpha-blended sprites on screen, moving randomly, which will do better? LWJGL or Java2d with performance flags? :stuck_out_tongue: my money is on LWJGL. (disclaimer: I use java2d for a living, lwjgl/games is a hobby.)

Since when did games have to use alpha blending or for that matter sprites? Draw 1000 pixel perfect circles on the screen in GL or Java2D. I think I’d be the other way in that contest simply due to primitives.

Tbh, I use LWJGL so I’m more than happy to accept its a better solution for high performance bitmap based gaming, but I don’t think its as cut and dry as your question makes it out to be.

How about:

Q) I’m trying to blit alot of sprites to the screen and I just can’t get the performance I want out of Java2D. Are there any other options?

A) Yes. JOGL/LWJGL provide bindings to OpenGL which is more complicated but gives a finer level of control and in most cases gives better performance. You might also consider GTGE which gives you an abstraction above Java2D/OpenGL.

Kev

I agree with the points you raised and I think I can manage to live with that Q/A entry :slight_smile:

http://www.cs.umd.edu/hcil/agile2d/ :o

Isn’t DirectX only supported on Windows? Either I am pretty sure OpenGL is supported on all platforms whereas DirectX is not!

The DirectX Java2D pipeline is/can obviously only used under Windows.

The OpenGL pipeline can be used pretty much everywhere. However, it still doesn’t behave very well (with 1.6).

Well wouldn’t it be better to compile in 1.5 with OpenGL where you have a larger audience than just windows instead of using 1.6 with DirectX and only being able to run in Windows!

You compile only once. You can use different pipelines via command line switches or through properties (set for specific operating systems) with jnlp.

Where can I learn LWJGL? Having used Java2D for all my programs, LWJGL’s javadoc looks very scary and complicated.