Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Resetting or Undoing AffineTransforms

 
Dan Falcone
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Swing / AWT forum.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell, thanks very much for the response. That's what I'll do then.

Much obliged...

- Dan
 
Dan Falcone
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell - I got a chance to try it and it worked perfectly. Thanks! Should have known that one... still learning.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic