使用AffineTransform将形状缩放/转换为给定的矩形 [英] Scaling/Translating a Shape to a given Rectangle using AffineTransform
问题描述
我试图用 AffineTransform 缩放/转换一个java.awt。 Shape ,以便将其绘制到定义的边界矩形中。
此外,我想在绘制具有' zoom '参数的区域中绘制它。
I尝试了AffineTransform的各种连接,但我找不到正确的序列。例如,下面的解决方案是错误的:
double zoom =(...); / *当前缩放* /
Rectangle2D viewRect =(...)/ **我们想要绘制形状的矩形* /
形状=(...)/ *应该放在矩形中的原始形状viewRect * /
Rectangle2D bounds = shape.getBounds2D();
double ratioW =(viewRect.getWidth()/ bounds.getWidth());
double ratioH =(viewRect.getHeight()/ bounds.getHeight());
AffineTransform转换[] =
{
AffineTransform.getScaleInstance(zoom,zoom),
AffineTransform.getTranslateInstance(-bounds.getX(), (),
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
请注意 AffineTransform
转换是以最常用的方式连接在一起的,这可能被认为是, first-out 顺序。可以在此示例中看到效果。根据下面的顺序,结果 Shape
首先被旋转,然后被缩放并最终被翻译。
at.translate(SIZE / 2,SIZE / 2);
at.scale(60,60);
at.rotate(Math.PI / 4);
return at.createTransformedShape(...);
I'm trying to scale/translate a java.awt.Shape with AffineTransform in order to draw it in a defined bounding Rectangle.
Moreover, I want to paint it in a drawing Area having a 'zoom' parameter.
I tried various concatenations of AffineTransform but I couldn't find the correct sequence. For example, the following solution was wrong:
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);
Any idea about the correct AffineTransform ?
Many thanks
Pierre
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(...);
这篇关于使用AffineTransform将形状缩放/转换为给定的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!