This returns an affine transform which is appropriate for modifying an existing one for a change in the window size. - Java 2D Graphics

Java examples for 2D Graphics:Transform

Description

This returns an affine transform which is appropriate for modifying an existing one for a change in the window size.

Demo Code


//package com.java2s;

import java.awt.geom.AffineTransform;

public class Main {


    /**/*from   ww w .j a va  2s  . c om*/
     * This returns an affine transform which is appropriate for
     * modifying an existing one for a change in the window size.
     * (NOTE: that this transform should be pre-concatenated with the
     * existing one!) The returned transform will satisfy the
     * following constraints: the centerpoint of the old window will
     * map to the center point of the new window, a uniform scaling
     * will be used in both the x and y-directions, the entire visible
     * region of in the old window will be visible in the new
     * window. */
    public static AffineTransform getResizeTransform(int oldWidth,
            int oldHeight, int newWidth, int newHeight) {

        // First calculate the scaling which is necessary. 
        double scale = Math.min((double) newWidth / (double) oldWidth,
                (double) newHeight / (double) oldHeight);

        // Now calculate the translation which is necessary.
        double tx = 0.5 * (newWidth - scale * oldWidth);
        double ty = 0.5 * (newHeight - scale * oldHeight);

        // Now create the transform and return it.
        return new AffineTransform(scale, 0., 0., scale, tx, ty);
    }
}

Related Tutorials