Scaling/Translating a Shape to a given Rectangle using AffineTransform
Note that AffineTransform
transformations are concatenated "in the most commonly useful way", which may be regarded as last in, first-out order. The effect can be seen in this example. Given the sequence below, the resulting Shape
is first rotated, then scaled and finally translated.
at.translate(SIZE/2, SIZE/2);
at.scale(60, 60);
at.rotate(Math.PI/4);
return at.createTransformedShape(...);
Inspired by trashgod's answer, the correct sequence was:
AffineTransform transforms[]=
{
AffineTransform.getScaleInstance(zoom, zoom),
AffineTransform.getTranslateInstance(viewRect.getX(),viewRect.getY()),
AffineTransform.getScaleInstance(ratioW, ratioH),
AffineTransform.getTranslateInstance(-bounds.getX(),-bounds.getY())
};
AffineTransform tr=new AffineTransform();
for(int i=0;i< transforms.length;++i)
{
tr.concatenate(transforms[i]);
}