This returns an affine transform which will center the given point in the window. - Java 2D Graphics

Java examples for 2D Graphics:Transform

Description

This returns an affine transform which will center the given point in the window.

Demo Code


//package com.java2s;

import java.awt.geom.AffineTransform;

public class Main {
    public static void main(String[] argv) throws Exception {
        int newX = 2;
        int newY = 2;
        int width = 2;
        int height = 2;
        System.out/* w w  w  .j a  va2s. co m*/
                .println(getCenteringTransform(newX, newY, width, height));
    }

    /**
     * This returns an affine transform which will center the given
     * point in the window.  (NOTE: that this transform should be
     * pre-concatenated with the existing one!)  The returned
     * transform will move the given point to the center and maintain
     * the x and y scales. */
    public static AffineTransform getCenteringTransform(int newX, int newY,
            int width, int height) {
        return new AffineTransform(1., 0., 0., 1., width / 2. - newX,
                height / 2. - newY);
    }
}

Related Tutorials