rotate BufferedImage - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Rotate

Description

rotate BufferedImage

Demo Code


//package com.java2s;
import java.awt.*;
import java.awt.image.*;

public class Main {
    public static BufferedImage rotate(BufferedImage img, int angle) {
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage dimg = new BufferedImage(w, h, img.getType());
        Graphics2D g = dimg.createGraphics();
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.drawImage(img, null, 0, 0);/*from ww  w .  j av a 2 s  .c  o  m*/
        return dimg;
    }

    public static BufferedImage rotate(BufferedImage img, double angle) {
        int w = img.getWidth();
        int h = img.getHeight();
        BufferedImage dimg = new BufferedImage(w, h, img.getType());
        Graphics2D g = dimg.createGraphics();
        g.rotate(angle, w / 2, h / 2);
        g.drawImage(img, null, 0, 0);
        return dimg;
    }
}

Related Tutorials