Java Image Rotate rotateImage(final Image img, final Point location, final double degrees)

Here you can find the source of rotateImage(final Image img, final Point location, final double degrees)

Description

Returns a rotated AffineTransform object that can be used to rotate an image.

License

Open Source License

Parameter

Parameter Description
img The Image to be rotated
location The location of the Image in the 2D coordinate space
degrees The number of degrees to rotate

Return

the rotated (and translated) AffineTransform

Declaration

public static AffineTransform rotateImage(final Image img, final Point location, final double degrees) 

Method Source Code


//package com.java2s;
/*// w ww . j  a  v  a2 s  .c o  m
 * Copyright ? 2011-2013 Brian Groenke
 * All rights reserved.
 * 
 *  This file is part of the 2DX Graphics Library.
 *
 *  This Source Code Form is subject to the terms of the
 *  Mozilla Public License, v. 2.0. If a copy of the MPL 
 *  was not distributed with this file, You can obtain one at 
 *  http://mozilla.org/MPL/2.0/.
 */

import java.awt.*;
import java.awt.geom.AffineTransform;

public class Main {
    /**
     * Returns a rotated AffineTransform object that can be used to rotate an
     * image.
     * 
     * @param img
     *            The Image to be rotated
     * @param location
     *            The location of the Image in the 2D coordinate space
     * @param degrees
     *            The number of degrees to rotate
     * @return the rotated (and translated) AffineTransform
     */
    public static AffineTransform rotateImage(final Image img, final Point location, final double degrees) {

        AffineTransform affine = new AffineTransform();
        affine.setToTranslation(location.getX(), location.getY());
        affine.rotate(Math.toRadians(degrees), img.getWidth(null) / 2, img.getHeight(null) / 2);
        return affine;
    }

    /**
     * Draws a rotated version of the given Image to the specified Graphics2D
     * object.
     * 
     * @param img
     *            The Image to be rotated
     * @param location
     *            The location of the Image in the 2D coordinate space
     * @param g2d
     *            The Graphics2D object on which to draw the new rotated image.
     * @param degrees
     *            The number of degrees to rotate
     * @return the rotated (and translated) AffineTransform
     */
    public static AffineTransform rotateImage(final Image img, final Point location, final Graphics2D g2d,
            final double degrees) {

        AffineTransform affine = new AffineTransform();
        affine.setToTranslation(location.getX(), location.getY());
        affine.rotate(Math.toRadians(degrees), img.getWidth(null) / 2, img.getHeight(null) / 2);
        g2d.drawImage(img, affine, null);
        return affine;
    }

    /**
     * Returns a rotated AffineTransform object that can be used to rotate an
     * image.
     * 
     * @param img
     *            The Image to be rotated
     * @param location
     *            The location of the Image in the 2D coordinate space
     * @param theta
     *            Rotation value
     * @param radians
     *            true if theta should be interpreted as a radian value, false
     *            if theta should be interpreted as a degree value.
     * @return the rotated (and translated) AffineTransform
     */
    public static AffineTransform rotateImage(final Image img, final Point location, final double theta,
            final boolean radians) {

        AffineTransform affine = new AffineTransform();
        affine.setToTranslation(location.getX(), location.getY());
        if (radians) {
            affine.rotate(theta, img.getWidth(null) / 2, img.getHeight(null) / 2);
        } else {
            affine.rotate(Math.toRadians(theta), img.getWidth(null) / 2, img.getHeight(null) / 2);
        }
        return affine;
    }

    /**
     * Draws a rotated version of the given Image to the specified Graphics2D
     * object.
     * 
     * @param img
     *            The Image to be rotated
     * @param location
     *            The location of the Image in the 2D coordinate space
     * @param g2d
     *            The Graphics2D object on which to draw the new rotated image.
     * @param theta
     *            Rotation value
     * @param radians
     *            true if theta should be interpreted as a radian value, false
     *            if theta should be interpreted as a degree value.
     * @return the rotated (and translated) AffineTransform
     */
    public static AffineTransform rotateImage(final Image img, final Point location, final Graphics2D g2d,
            final double theta, final boolean radians) {

        AffineTransform affine = new AffineTransform();
        affine.setToTranslation(location.getX(), location.getY());
        if (radians) {
            affine.rotate(theta, img.getWidth(null) / 2, img.getHeight(null) / 2);
        } else {
            affine.rotate(Math.toRadians(theta), img.getWidth(null) / 2, img.getHeight(null) / 2);
        }
        g2d.drawImage(img, affine, null);
        return affine;
    }
}

Related

  1. rotate(Image img, double angle)
  2. rotate(Image img, double angle)
  3. rotate(Image img, float radian)
  4. Rotate(Image src, int angel)
  5. rotateImage(final File imagePath, int numquadrants)
  6. rotateImage(Image inImage, int inMaxWidth, int inMaxHeight, int inRotationDegrees)
  7. rotateImage(Image originalImage, double theta)
  8. rotateImage(String img_fn, int orient, String dest_fn)