Scales the shape, without changing its coordinates. - Java 2D Graphics

Java examples for 2D Graphics:Shape

Description

Scales the shape, without changing its coordinates.

Demo Code


//package com.java2s;

import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

public class Main {
    /**/*  www . ja v a 2 s . c  o  m*/
     * Scales the shape, without changing its coordinates.
     * Additional a translation can be made, using the two
     * translation values.
     * 
     * @param shape The shape, which should be scaled.
     * @param scale The scale.
     * @param translationX A translation on the x axis.
     * @param translationY A translation on the y axis.
     * @return The scaled shape.
     */
    public static final Shape scaleShape(Shape shape, double scale,
            double translationX, double translationY) {

        Rectangle2D bounds = shape.getBounds2D();
        AffineTransform af = AffineTransform.getTranslateInstance(
                0 - bounds.getX(), 0 - bounds.getY());
        // apply normalisation translation ...
        Shape s = af.createTransformedShape(shape);

        af = AffineTransform.getScaleInstance(scale, scale);
        // apply scaling ...
        s = af.createTransformedShape(s);

        // now retranslate the shape to its original position ...
        af = AffineTransform.getTranslateInstance(bounds.getX()
                - translationX, bounds.getY() - translationY);
        return af.createTransformedShape(s);
    }
}

Related Tutorials