protected AffineTransform createTransforms(Image image, Dimension dimension) {
        int imageHeight = image.getHeight(null);
        int imageWidth = image.getWidth(null);
        AffineTransform returnValue = new AffineTransform();

        if ((imageHeight > dimension.getHeight()) || 
            (imageWidth > dimension.getWidth())) {

            // Scaling will be necessary.
            double scaleFactor = Math.min(dimension.getHeight() / imageHeight, 
                dimension.getWidth() / imageWidth);
            
            // Concatenate a scaling transform on.
            AffineTransform temp = new AffineTransform();
            temp.setToScale(scaleFactor, scaleFactor);
            returnValue.concatenate(temp);
            
            // Recalculate the image height and width based on the scale factor.
            // We need to do this because we might need to horizontally or
            // vertically center the image.
            imageHeight *= scaleFactor;
            imageWidth *= scaleFactor;
        }
        
        // Note that it is possible to need both scaling and centering so we
        // always do our scaling first and then after we know our final
        // dimensions for the image we do our centering.
        if ((imageHeight < dimension.getHeight()) || 
            (imageWidth < dimension.getWidth())) {
            
            // Centering will be necessary.
            AffineTransform temp = new AffineTransform();
            temp.setToTranslation((dimension.getWidth() - imageWidth) / 
                2, (dimension.getHeight() - imageHeight) / 2);
            returnValue.concatenate(temp);
        }
        
        return returnValue;
    }