Do a scaling transformation around the anchor point - Java java.lang

Java examples for java.lang:Math Geometry

Description

Do a scaling transformation around the anchor point

Demo Code


//package com.java2s;
import java.awt.Shape;
import java.awt.geom.AffineTransform;

import java.awt.geom.Point2D;

public class Main {
    /**/*from   www  .j  a v  a  2 s . c  om*/
     * 
     * Do a scaling transformation around the anchor point
     * 
     * @param shape
     * @param scalingFactor
     * @param anchorPoint
     *            can be null
     * @return null if either shape is null or scaling factor is zero
     */
    public static Shape getScaledShape(final Shape shape,
            double scalingFactor, Point2D anchorPoint) {
        if (shape == null || scalingFactor == 0) {
            return null;
        }

        AffineTransform scaleTransform = new AffineTransform(); // Identity transformation.

        if (scalingFactor != 1) {
            if (anchorPoint != null) {
                scaleTransform.translate(anchorPoint.getX(),
                        anchorPoint.getY());
            }

            scaleTransform.scale(scalingFactor, scalingFactor);

            if (anchorPoint != null) {
                scaleTransform.translate(-anchorPoint.getX(),
                        -anchorPoint.getY());
            }
        }

        return scaleTransform.createTransformedShape(shape);
    }
}

Related Tutorials