Here you can find the source of rotateImage(BufferedImage img, double angle, int type)
public static BufferedImage rotateImage(BufferedImage img, double angle, int type)
//package com.java2s; /* VisNow/* ww w . j a v a2s. c om*/ Copyright (C) 2006-2013 University of Warsaw, ICM This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the University of Warsaw, Interdisciplinary Centre for Mathematical and Computational Modelling, Pawinskiego 5a, 02-106 Warsaw, Poland. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.*; public class Main { public static BufferedImage rotateImage(BufferedImage img, double angle, int type) { return rotateImage(img, angle, type, Color.BLACK); } public static BufferedImage rotateImage(BufferedImage img, double angle, int type, Color fillBgColor) { if (img == null) { return null; } if (angle > 360.0 || angle < -360) { angle = angle % 360.0; } if (angle < 0) { angle = 360 + angle; } if (angle == 0.0 || angle == 360.0) { return img; } //System.out.println("angle="+angle); int w = img.getWidth(); int h = img.getHeight(); /* AffineTransform tr = new AffineTransform(); tr.rotate(theta,w/2,h/2); BufferedImageOp op = new AffineTransformOp(tr, type); BufferedImage out = op.filter(img,null); */ /* AffineTransform tr = new AffineTransform(); tr.rotate(theta, w/2.0, h/2.0); AffineTransform translationTransform = findTranslation(tr, img); tr.preConcatenate(translationTransform); BufferedImageOp op = new AffineTransformOp(tr, type); BufferedImage out = op.filter(img,null); */ BufferedImage out = null; if (angle == 90.0 || angle == 180.0 || angle == 270.0) { switch ((int) angle) { case 90: out = new BufferedImage(h, w, img.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { out.setRGB(h - y - 1, x, img.getRGB(x, y)); } } break; case 180: out = new BufferedImage(w, h, img.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { out.setRGB(w - x - 1, h - y - 1, img.getRGB(x, y)); } } break; case 270: out = new BufferedImage(h, w, img.getType()); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { out.setRGB(y, w - x - 1, img.getRGB(x, y)); } } break; } } else { double theta = angle * Math.PI / 180.0; int neww = w, newh = h; double dx = 0.0, dy = 0.0; double s = Math.sin(theta); double c = Math.cos(theta); if (angle > 0.0 && angle < 90.0) { neww = (int) Math.round(((double) w) * c + ((double) h) * s); newh = (int) Math.round(((double) w) * s + ((double) h) * c); dx = ((double) h) * s; dy = 0.0; } else if (angle > 90.0 && angle < 180.0) { neww = (int) Math.round(-((double) w) * c + ((double) h) * s); newh = (int) Math.round(((double) w) * s - ((double) h) * c); dx = -((double) w) * c + ((double) h) * s; dy = -((double) h) * c; } else if (angle > 180.0 && angle < 270.0) { neww = (int) Math.round(-((double) w) * c - ((double) h) * s); newh = (int) Math.round(-((double) w) * s - ((double) h) * c); dx = -((double) w) * c; dy = -((double) w) * s - ((double) h) * c; } else if (angle > 270.0 && angle < 360.0) { neww = (int) Math.round(((double) w) * c - ((double) h) * s); newh = (int) Math.round(-((double) w) * s + ((double) h) * c); dx = 0.0; dy = -((double) w) * s; } AffineTransform tr = new AffineTransform(); tr.translate(dx, dy); tr.rotate(theta); BufferedImageOp op = new AffineTransformOp(tr, type); out = new BufferedImage(neww, newh, img.getType()); Graphics2D g2d = (Graphics2D) out.getGraphics(); Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight()); g2d.setPaint(fillBgColor); g2d.fill(clear); op.filter(img, out); } return out; } }