Java 怎么修改提供的仿射变换,使实数边界给定的矩形适合窗口边界给定的矩形

2019年7月16日21:56:02 评论 800字阅读2分40秒

1.简介

Java 怎么修改提供的仿射变换,使实数边界给定的矩形适合窗口边界给定的矩形?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.awt.geom.AffineTransform;
import java.awt.geom.RectangularShape;

public class Main {
    
    public static void getFittingTransform(AffineTransform transform,
            RectangularShape realBounds, RectangularShape windowBounds) {

        if (realBounds == null || windowBounds == null) {
            transform.setToIdentity();
        } else {

            
            double realWidth = realBounds.getWidth();
            double realHeight = realBounds.getHeight();
            double windowWidth = windowBounds.getWidth();
            double windowHeight = windowBounds.getHeight();

            if (realWidth > 0 && realHeight > 0) {

                
                double scaleWidth = windowWidth / realWidth;
                double scaleHeight = windowHeight / realHeight;
                double scale = Math.min(scaleWidth, scaleHeight);

                transform.setTransform(scale, 0., 0., -scale,
                        windowWidth / 2., windowHeight / 2.);
            } else {
                transform.setToIdentity();
            }
        }
    }
}

 
  • 版权所有:编程十万个怎么办
  • 转载注明:https://www.tah1986.com/20974.html
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: