Wednesday, September 22, 2010

Two Java rotates - one works one does not (sometimes)

Why does this code not always work?
        g2.translate(translate.width, translate.height);
        g2.rotate((myRotate * Math.PI) / 180, 0, 0);
        g2.drawImage(image, 0, 0, null);

This code appears to work in all our test situation

        AffineTransform trans = AffineTransform.getTranslateInstance(translate.width, translate.height);
        trans.rotate((myRotate * Math.PI) / 180);
        g2.transform(trans);
        g2.drawImage(image, 0, 0, null);

This came up as a client issue. I did not write the original code but I was asked to help track down and resolve this rather annoying issue. When I run the old code on my machine it runs just fine. When we run the old code on a VM on another machine it works fine but running it as a servlet under Tomcat and it screws up and does not generate a rotated image. You get an empty rectangle instead.

The problem is now fully fixed but it just seems odd that the AffineTransform works where the standard rotate does not. You don't get any errors / exceptions. You just don't get a rotated image. We are going to run some timing tests between the two calls as we are curious which is faster. Of course faster and broken is broken so we will not switch back to the original code but we want to make sure server performance is not impacted massively with the new code in place. If this was client side processing we would be less worried but you still want things to run as fast as possible.

No comments:

Post a Comment