Graphics2D and AffineTransform needs object-reuse for smooth animation!
Hi,
I'm currently working on a graphical framework for animation using Java2D but has come to a dead end. The goal of the framework is to deliver smooth animation on various platforms, but this seems impossible due to the following fact:
I have a tree of graphical objects i render on a Graphics2D-object. Some of the objects to be rendered are transforms on the Graphics2D instead of visible objects - this way I can have transform-objects in my tree which will affect all child-objects. This is all very nice, but when doing transformations on the Graphics2D *A LOT* of objects are being created by the implementation of Graphics2D (SunGraphics2D). I've designed my framework to utilize object-reuse and cacheing-mechanisms to ensure no garbage collection is performed when actual animation is in progress - if gc's are performed, this results in visible pauses in the animation. Now, I would like to ask if someone knows how to get around this problem, or suggest I simply abandon Java2D?
The details of my problem is the following:
When doing transforms on the Graphics2D-object which is passed to every object in the tree (and hence, a lot of transformations are being done), a lot of FontInfo-objects are being created - even though I don't use any of them - it's all in the subsystem. The source in the SunGraphics2D is as follows:
// this is called by my framework to rotate all childs in the tree
publicvoid rotate(double d){
transform.rotate(d);
invalidateTransform();// the evil starts here
}
// this is called a lot of places in SunGraphics2D
protectedvoid invalidateTransform(){
// a lot is thigs are going on in this method - cutted out...
// before this method returns, the following takes place
fontInfo = checkFontInfo(null, font);// now we are getting there
}
// this is the method of pure evil object allocations
public FontInfo checkFontInfo(FontInfo fontinfo, Font font1){
// every time this method is called, a FontInfo-object is allocated
FontInfo fontinfo1 =new FontInfo();
// and a lot of other objects are being created as well...
}
I have come to think, that Java2D is pretty old and should be pretty mature at this point, but now I doubt it since object-reuse is a pretty obvious way of doing optimizations.
Has any of you experienced the same problem or maybe found a solution to doing transformations on a Graphics2D-object without a ton of objects being created?
If you would like to have a look at the problem you can do the following:
Make yourself a little program which is doing some transforms on a Graphics2D-object in a loop (to emulate the 25fps animation and the transform-objects in the tree). Now use your favorite memory profiler (I use JProbe Memory Profiler, free evaluation) and see for yourself - the objects which are garbage collected includes a ton of FontInfo-objects and many AffineTransform-objects.
If I do not find any solution to this problem, I'm forced to face the fact, that Java2D is not suitable for animation-purposes - gc's during animation is no solution!
Thank you for your time - hope to hear from you soon.
Regards,
// x-otic.

