Here you can find the source of rotateImage(final Image img, final Point location, final double degrees)
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 |
public static AffineTransform rotateImage(final Image img, final Point location, final double degrees)
//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; } }