Scaling/Translating a Shape to a given Rectangle using AffineTransform

后端 未结 2 1300
梦如初夏
梦如初夏 2020-12-02 00:54

I\'m trying to scale/translate a java.awt.Shape with AffineTransform in order to draw it in a defined bounding Rectangle.

Moreover,

相关标签:
2条回答
  • 2020-12-02 01:49

    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]);
     }
    
    0 讨论(0)
  • 2020-12-02 01:50

    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(...);
    
    0 讨论(0)
提交回复
热议问题