Here you can find the source of rotateImageRect(final BufferedImage image, final int degrees)
public static BufferedImage rotateImageRect(final BufferedImage image, final int degrees)
//package com.java2s; /**//from w w w . jav a 2s.c om * * Copyright 2008 - 2009 * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. * * @project loonframework * @author chenpeng * @email?ceponline@yahoo.com.cn * @version 0.1 */ import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; public class Main { public static BufferedImage rotateImageRect(final BufferedImage image, final int degrees) { double phi = Math.toRadians(degrees); int width = image.getWidth(null); int height = image.getHeight(null); Point a = new Point(0, 0); Point b = new Point(width, 0); Point c = new Point(0, height); Point d = new Point(width, height); Point newA = rotate(a, phi); Point newB = rotate(b, phi); Point newC = rotate(c, phi); Point newD = rotate(d, phi); int w = Math.max(Math.max(newA.x, newB.x), Math.max(newC.x, newD.x)) - Math.min(Math.min(newA.x, newB.x), Math.min(newC.x, newD.x)); int h = Math.max(Math.max(newA.y, newB.y), Math.max(newC.y, newD.y)) - Math.min(Math.min(newA.y, newB.y), Math.min(newC.y, newD.y)); Rectangle rect = new Rectangle(0, 0, w, h); BufferedImage img = new BufferedImage(rect.width, rect.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = img.createGraphics(); g.setTransform(AffineTransform.getRotateInstance(phi, rect.width / 2, rect.height / 2)); g.drawImage(image, (rect.width - width) / 2, (rect.height - height) / 2, null); g.dispose(); return img; } private static Point rotate(Point p, double angle) { double c = Math.cos(angle); double s = Math.sin(angle); return new Point((int) (p.x * c - p.y * s), (int) (p.x * s + p.y * c)); } }