This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Resetting or Undoing AffineTransforms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Resetting or Undoing AffineTransforms" Watch "Resetting or Undoing AffineTransforms" New topic
Author

Resetting or Undoing AffineTransforms

Dan Falcone
Greenhorn

Joined: Feb 03, 2009
Posts: 8
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?

Thanks for any input.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19685
    
  20

Moving to the Swing / AWT forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
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.
Dan Falcone
Greenhorn

Joined: Feb 03, 2009
Posts: 8
Campbell, thanks very much for the response. That's what I'll do then.

Much obliged...

- Dan
Dan Falcone
Greenhorn

Joined: Feb 03, 2009
Posts: 8
Campbell - I got a chance to try it and it worked perfectly. Thanks! Should have known that one... still learning.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
You're welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Resetting or Undoing AffineTransforms