Here you can find the source of rotate(BufferedImage img, int angle)
public static BufferedImage rotate(BufferedImage img, int angle)
//package com.java2s; //License from project: Open Source License import java.awt.Graphics2D; import java.awt.RenderingHints; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; public class Main { public static BufferedImage rotate(BufferedImage img, int angle) { AffineTransform trans;// www . j a v a 2 s .c om BufferedImage newImg; int w = img.getWidth(); int h = img.getHeight(); if (angle == 0) { newImg = new BufferedImage(w, h, img.getType()); trans = null; } else if (angle == 90) { newImg = new BufferedImage(h, w, img.getType()); trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, h, 0.0); } else if (angle == 180) { newImg = new BufferedImage(w, h, img.getType()); trans = new AffineTransform(-1.0, 0.0, 0.0, -1.0, w, h); } else if (angle == 270) { newImg = new BufferedImage(h, w, img.getType()); trans = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, w); } else { throw new RuntimeException("Invalid angle " + angle); } Graphics2D g = newImg.createGraphics(); try { if (trans != null) { g.transform(trans); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } g.drawImage(img, 0, 0, w, h, null); } finally { g.dispose(); } return newImg; } }