Java BufferedImage Rotate rotate(BufferedImage img, int angle)

Here you can find the source of rotate(BufferedImage img, int angle)

Description

rotate

License

Open Source License

Declaration

public static BufferedImage rotate(BufferedImage img, int angle) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage rotate(BufferedImage img, int angle) {
        double sin = Math.abs(Math.sin(Math.toRadians(angle))), cos = Math.abs(Math.cos(Math.toRadians(angle)));
        int w = img.getWidth(null), h = img.getHeight(null);
        int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
        BufferedImage bimg = new BufferedImage(neww, newh, img.getType());
        Graphics2D g = bimg.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.drawRenderedImage(img, null);/*  w  w  w.j a  v a 2  s  . co m*/
        g.dispose();
        return bimg;
    }
}

Related

  1. rotate(BufferedImage image, float theRadiansAngle)
  2. rotate(BufferedImage image, int degrees)
  3. rotate(BufferedImage img, int angle)
  4. rotate(BufferedImage img, int angle)
  5. rotate(BufferedImage img, int angle)
  6. rotate(BufferedImage img, int b, int a, int angle)
  7. rotate(BufferedImage source, int angle)
  8. rotate(BufferedImage src)
  9. rotate(final BufferedImage image, final int angle)