• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Resetting or Undoing AffineTransforms

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Swing / AWT forum.
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic