我正在尝试使用AffineTransform来缩放/平移java.awt.Shape,以便将其绘制在一个定义好的边界矩形内。此外,我希望能够在具有“缩放”参数的绘图区域中进行绘制。
我尝试过多种AffineTransform的组合,但是我无法找到正确的顺序。例如,下面的解决方案是错误的:
我尝试过多种AffineTransform的组合,但是我无法找到正确的顺序。例如,下面的解决方案是错误的:
double zoom=(...);/* current zoom */
Rectangle2D viewRect=(...)/** the rectangle where we want to paint the shape */
Shape shape=(...)/* the original shape that should fit in the rectangle viewRect */
Rectangle2D bounds=shape.getBounds2D();
double ratioW=(viewRect.getWidth()/bounds.getWidth());
double ratioH=(viewRect.getHeight()/bounds.getHeight());
AffineTransform transforms[]=
{
AffineTransform.getScaleInstance(zoom, zoom),
AffineTransform.getTranslateInstance(-bounds.getX(),-bounds.getY()),
AffineTransform.getTranslateInstance(viewRect.getX(),viewRect.getY()),
AffineTransform.getScaleInstance(ratioW, ratioH)
};
AffineTransform tr=new AffineTransform();
for(int i=0;i< transforms.length;++i)
{
tr.concatenate(transforms[i]);
}
Shape shape2=tr.createTransformedShape(shape);
graphics2D.draw(shape2);
请问正确的AffineTransform是什么?
非常感谢!
Pierre