使用AffineTransform将形状缩放/转换为给定的矩形(Scaling/Translating a Shape to a given Rectangle using AffineTransfo

编程入门 行业动态 更新时间:2024-10-24 14:21:00
使用AffineTransform将形状缩放/转换为给定的矩形(Scaling/Translating a Shape to a given Rectangle using AffineTransform)

我正在尝试缩放/翻译java.awt。 使用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的想法?

非常感谢

皮埃尔

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

最满意答案

请注意, AffineTransform转换以“最常用的方式”连接在一起,这可能会被视为最后的 先进顺序。 在这个例子中可以看到效果。 根据下面的顺序,生成的Shape首先被旋转,然后被缩放并最终被翻译。

at.translate(SIZE/2, SIZE/2); at.scale(60, 60); at.rotate(Math.PI/4); return at.createTransformedShape(...);

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(...);

更多推荐

本文发布于:2023-07-15 21:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1119325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩形   缩放   转换为   形状   AffineTransform

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!