Java examples for 2D Graphics:BufferedImage Create
Creates and returns a buffered image that is a rotated version of a specified buffered image.
//package com.java2s; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.awt.image.BufferedImageOp; public class Main { /**// w ww.ja v a 2s. c o m * Creates and returns a buffered image that is a rotated version of a specified * buffered image. The transform is done so that the image is not truncated, and * occupies the minimum bounding box * * @param bImage * @param theta Angle the image is to be rotated, in radians. * @return the rotated image. */ public static BufferedImage getRotatedImage(BufferedImage bImage, double theta) { // Normalize theta to be between 0 and PI*2 theta = ((theta % (Math.PI * 2)) + Math.PI * 2) % (Math.PI * 2); double x = 0; double y = 0; double alpha = 0; double w = bImage.getWidth(); double h = bImage.getHeight(); if (theta >= 0 && theta <= Math.PI / 2) { x = h * Math.sin(theta); y = 0; } if (theta > Math.PI / 2 && theta <= Math.PI) { alpha = theta - Math.PI / 2; x = w * Math.sin(alpha) + h * Math.cos(alpha); y = h * Math.sin(alpha); } if (theta > Math.PI && theta <= Math.PI * 3 / 2) { alpha = theta - Math.PI; x = w * Math.cos(alpha); y = w * Math.sin(alpha) + h * Math.cos(alpha); } // Works if (theta > Math.PI * 3 / 2 && theta <= Math.PI * 2) { alpha = Math.PI * 2 - theta; x = 0; y = w * Math.sin(alpha); } AffineTransform atx = AffineTransform.getTranslateInstance(x, y); atx.rotate(theta); BufferedImageOp op = new AffineTransformOp(atx, AffineTransformOp.TYPE_BILINEAR); BufferedImage result = op.filter(bImage, null); return result; } }