This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In a paintComponent method I'm calling g.drawImage several times, rotating some images with AffineTransform but not others, something like this, with object MyImage containing fields
boolean rotate; // true if image is to be rotated
x, y; // upper left position to place image
width, height; // of image
BufferedImage bi; // image itself
I understand that successive assignments of these transforms are cumulative, but (I'm new to this obviously) I don't want it to be here. What is the accepted method of "resetting" after each one? I see the createInverse method which I'm guessing is for other uses, not this. Is it necessary to use an AffineTransform instance static method? I've tried but with no success. Is it better to use a new graphics object?
Don't even bother trying to reset an AffineTransform. Get a duplicate of the Graphics object, which I think you get with its create() method, and apply the transforms to that. The reason is that AffineTransforms use floating point arithmetic and the repeated calculations when reversed still have tiny errors in. These are enough to prevent the Graphics object reverting exactly to its initial state. The same applies to methods like Graphics#shear() #translate #scale and #rotate. The shear method appears to be the most complicated, and causes the most problems like that. Translate simply adds numbers to the x and y values, and appears to be easy to reverse.
You may need to cast your Graphics object to Graphics2D to gain access to some of those methods.
Joined: Feb 03, 2009
Campbell, thanks very much for the response. That's what I'll do then.
Joined: Feb 03, 2009
Campbell - I got a chance to try it and it worked perfectly. Thanks! Should have known that one... still learning.