AffineTransform#createTransformedShape(GeneralPath)

Would appreciate some help with the following please:

My application which works correctly with jdk1.5.0_06 fails with the following ClassCastException with Mustang b85 (-source 1.5):


java.lang.ClassCastException: java.awt.geom.Path2D$Double cannot be cast to java.awt.geom.GeneralPath

when I try to retrieve a transformed GeneralPath as follows:


GeneralPath outputGeneralPath  = (GeneralPath)affineTransform.createTransformedShape( inputGeneralPath );

From a quick look at the source code for 1.5 and 1.6 for the method createTransformedShape(…), it appears that 1.5 can conditionally return a GeneralPath, while 1.6 unconditionally returns a Path2D.Double, and the exception occurs because GeneralPath extends Path2D.Float.

Am I missing anythin’ obvious ?

Thanks in advance

In trying to make the cast to a GeneralPath, you’re making an unsafe assumption about the implementation of createTransformedShape(), which is liable to change at any time.

Hows about coding to the API -rather than the implementation- like so:

GeneralPath outputGeneralPath = new GeneralPath( affineTransform.createTransformedShape( inputGeneralPath ) );

Bleb is correct - you shouldn’t cast the return of createTransformedShape().

Here’s the fix which caused the change:
4172661: GeneralPath needs double version
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4172661

Thanks,
Dmitri

Thanks for the quick help guys ! alright, I get the big picture now after reading that Bug Fix.
After looking at the 1.5 docs again, I think I could actually get away with clobbering the input data with an in-place transform by doing inputGeneralPath.transform( affineTransform ) since I really didn’t need to keep the original data around. I’m using the GeneralPath to build a couple of hand-crafted icons to display in the ToolBar, and the application was crashing right at the start-up.

Thanks again !