Rotates a given shape. - Java 2D Graphics

Java examples for 2D Graphics:Shape

Description

Rotates a given shape.

Demo Code


//package com.java2s;
import java.awt.Point;

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

public class Main {
    /**/*w w w.ja  v  a  2  s  . c  o  m*/
     * Rotates a given shape.
     * 
     * @param shape The shape to rotate.
     * @param rotation The rotation.
     * @return The rotated shape.
     */
    public static final Shape rotateShape(Shape shape, double rotation) {
        AffineTransform transform = new AffineTransform();

        transform.rotate(Math.toRadians(rotation), shape.getBounds()
                .getCenterX(), shape.getBounds().getCenterY());

        return transform.createTransformedShape(shape);
    }

    /**
     * Rotates a given shape around a given point.
     * 
     * @param shape The shape to rotate.
     * @param rotation The rotation.
     * @param rotationPoint The point the shape is rotated around.
     * @return The rotated shape.
     */
    public static final Shape rotateShape(Shape shape, double rotation,
            Point rotationPoint) {
        AffineTransform transform = new AffineTransform();

        transform.rotate(Math.toRadians(rotation), rotationPoint.x,
                rotationPoint.y);

        return transform.createTransformedShape(shape);
    }

    /**
     * Rotates a given shape around a given point.
     * 
     * @param shape The shape to rotate.
     * @param rotation The rotation.
     * @param pointX The x coordinate of the rotation point.
     * @param pointY The y coordinate of the rotation point.
     * @return The rotated shape.
     */
    public static final Shape rotateShape(Shape shape, double rotation,
            double pointX, double pointY) {
        AffineTransform transform = new AffineTransform();

        transform.rotate(Math.toRadians(rotation), pointX, pointY);

        return transform.createTransformedShape(shape);
    }
}

Related Tutorials