aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes AffineTransform Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "AffineTransform "transform" method" Watch "AffineTransform "transform" method" New topic
Author

AffineTransform "transform" method

Arron Ferguson
Greenhorn

Joined: Feb 01, 2005
Posts: 28
I have been scouring the web looking for examples and documentation concerning the java.awt.geom.AffineTransform class's overloaded transform method - specifically this one:



The Javadoc is sketchy at best on how to use this method (same with the Java 2D tutorial - no mention of how to use the method). I'm not sure whether this method is suppose to take a matrix or if it's suppose to take a set of arbitrary points which can then be used to transform (e.g. warp) an image (I'm hoping the latter).

What I'm trying to do is take an image, rotate it by 45 degrees, squish the image so that this diamond shape is now 2h = w. Basically the pseudo isometric tile (pseudo because true isometric has all 3 angles being equal and a rise over run of 1:2 is actually dimetric due to only 2 sides being the same). I've managed to do the shearing correctly:


And so on. I want to be able to do this with the standard Java 2 SE and not use advanced imaging or Java 3D.

Any help, tips, links, advice, or even code snippets most welcome.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Don't know if its any better than the API, but the Transforming Shapes, Text, and Images tutorial might help.
Arron Ferguson
Greenhorn

Joined: Feb 01, 2005
Posts: 28
Rob Camick wrote:Don't know if its any better than the API, but the Transforming Shapes, Text, and Images tutorial might help.


Thanks Rob for a quick reply. Yeah, I did look at this and it doesn't go beyond scratching the surface and so no coverage of the transform methods and what the values fed to it are for.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Suggest you try copying the Graphics object with its create() method. You might have to cast it to Graphics2D. Then use its scale, rotate, translate and shear methods. The API for AffineTransform tells you which matrix operations they use for the different transforms.
Arron Ferguson
Greenhorn

Joined: Feb 01, 2005
Posts: 28
Campbell Ritchie wrote:Suggest you try copying the Graphics object with its create() method. You might have to cast it to Graphics2D. Then use its scale, rotate, translate and shear methods. The API for AffineTransform tells you which matrix operations they use for the different transforms.


But then I'm faced with the entire graphics context having the transforms/translations applied to it rather than local transforms/translations.
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
not sure whether this method is suppose to take a matrix or if it's suppose to take a set of arbitrary points
AffineTransform contains the matrix and does the matrix math in the background. The transform method takes an arry of arbitrary points (in any space) and transforms them into the space defined by the AffineTransform.
Arron Ferguson
Greenhorn

Joined: Feb 01, 2005
Posts: 28
Craig Wood wrote:not sure whether this method is suppose to take a matrix or if it's suppose to take a set of arbitrary points
AffineTransform contains the matrix and does the matrix math in the background. The transform method takes an arry of arbitrary points (in any space) and transforms them into the space defined by the AffineTransform.


Wow, Craig, thanks, this is exactly what I needed. Thank you very much for your time in answering my question.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Arron Ferguson wrote:But then I'm faced with the entire graphics context having the transforms/translations applied to it rather than local transforms/translations.
No, you only apply the transforms to the copy object.

You need a copy object regardless; you can transform and un-transform a Graphics object, but the floating-point arithmetic is never quite precise, so your Graphics will be slightly skewed afterwards. That seems to be more of a problem with rotation and shearing than scaling or translation.

And sorry for not replying earlier. And Craig Wood's code always works well, doesn't it
Arron Ferguson
Greenhorn

Joined: Feb 01, 2005
Posts: 28
Campbell Ritchie wrote:
Arron Ferguson wrote:But then I'm faced with the entire graphics context having the transforms/translations applied to it rather than local transforms/translations.
No, you only apply the transforms to the copy object.

You need a copy object regardless; you can transform and un-transform a Graphics object, but the floating-point arithmetic is never quite precise, so your Graphics will be slightly skewed afterwards. That seems to be more of a problem with rotation and shearing than scaling or translation.

And sorry for not replying earlier. And Craig Wood's code always works well, doesn't it


Ah yes, I saw that afterwards and realized that what you just said is correct. Whether I use the AffineTransform or the methods of the Graphics2D class you can always reset it. Talking about not precise, I have yet to see a differences in quality for rotation/shear/scale. For example:



Has absolutely no affect (or effect) on images that have been sheared, scaled, or rotated. I've tested this both on Linux (Ubuntu) as well as Vista - both using JRE 6 u10. Both computers show no change between using rendering hints either on the Graphics2D object nor on an AffineTransformOp. I've compared screen captures and zoomed in to see differences and there are none.

I came across a few bug reports on the Sun site suggesting that a couple of years back bicubic wasn't implemented and that for printing these values had undesirable artefacts but nothing particularly suggesting anything wrong with rendering hints not working. I'm less apt to file a bug report since the last few I've posted in the last few months don't get addressed - so I'll just use what works and move on.

For affine transforms in Java, I came across a fellow who made a utility class called AffineTransformUtil and it helped me to see what was being done.

Additionally for arbitrary transforms to images, I came across Jerry Huxtable's web page that has a library (under the Apache License) which contains a perspective filter - which was really what this whole adventure was all about: creating arbitrary (and stationary) sides to a cube based on images fed into the UI. Sometimes the search through Google takes one through some interesting paths.

And yes, Craig Wood's code works and is easy to follow.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You might be lucky and manage to un-shear a Graphics object, or you can be unlucky and have the problems I had once where everything else "after" the shearing was slightly out of place. I didn't realise until too late that a copy object would have sorted out that problem. And it was an assignment, but I still got a good mark.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AffineTransform "transform" method