• 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

AffineTransform "transform" method

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know if its any better than the API, but the Transforming Shapes, Text, and Images tutorial might help.
 
Arron Ferguson
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic