我試圖縮放/轉換java.awt。 形狀與AffineTransform爲了繪製它在一個定義的邊界矩形。使用AffineTransform縮放/將形狀轉換爲給定的矩形
此外,我想要繪製在具有'zoom'參數的繪圖區域中。
我嘗試過各種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 2010-10-02 12:18:11
這對推理有用的概念關於結果;但是,當然,沒有_queue_,只包含一系列操作的變換。 – trashgod 2010-10-02 13:01:31