Java examples for 2D Graphics:Transform
This modifies the supplied affine transform so that the rectangle given by real Bounds will fit inside of the rectangle given by window Bounds.
//package com.java2s; import java.awt.geom.AffineTransform; import java.awt.geom.RectangularShape; public class Main { /**//from ww w . j a v a2 s . co m * This modifies the supplied affine transform so that the * rectangle given by realBounds will fit inside of the rectangle * given by windowBounds. The center of the realBounds rectangle * will coincide with that of the windowBounds rectangle. * * NOTE: THIS ONLY CORRECTLY HANDLES THE CASE WHEN THE USER SPACE * RECTANGLE IS CENTERED ON THE ORIGIN. * * @param transform the transform which will be modified * @param realBounds the user space rectangle * @param windowBounds the window to map the user rectangle to */ public static void getFittingTransform(AffineTransform transform, RectangularShape realBounds, RectangularShape windowBounds) { if (realBounds == null || windowBounds == null) { transform.setToIdentity(); } else { // Get the dimensions of the windows. double realWidth = realBounds.getWidth(); double realHeight = realBounds.getHeight(); double windowWidth = windowBounds.getWidth(); double windowHeight = windowBounds.getHeight(); if (realWidth > 0 && realHeight > 0) { // Get the necessary scaling factor. 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(); } } } }