Java BufferedImage Rotate rotate(BufferedImage image, double theta)

Here you can find the source of rotate(BufferedImage image, double theta)

Description

rotate

License

Open Source License

Declaration

public static BufferedImage rotate(BufferedImage image, double theta) 

Method Source Code

//package com.java2s;
/* /*from   ww  w  . j av  a2 s . c o m*/
    
 Created on Mar 4, 2005
    
 The Bungee View applet lets you search, browse, and data-mine an image collection.  
 Copyright (C) 2006  Mark Derthick
    
 This program 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
 of the License, or (at your option) any later version.  See gpl.html.
    
 This program 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 this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    
 You may also contact the author at 
 mad@cs.cmu.edu, 
 or at
 Mark Derthick
 Carnegie-Mellon University
 Human-Computer Interaction Institute
 Pittsburgh, PA 15213
    
 */

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;

import java.awt.Transparency;

import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

public class Main {
    private static final GraphicsConfiguration graphicsConfiguration = GraphicsEnvironment
            .getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

    public static BufferedImage rotate(BufferedImage image, double theta) {
        boolean swap = Math.round(2.0 * theta / Math.PI) % 2 == 1;
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        double delta = swap ? (w - h) / 2.0 : 0.0;
        AffineTransform at = AffineTransform.getTranslateInstance(-delta, delta);
        at.rotate(theta, w / 2.0, h / 2.0);
        BufferedImage rotated = swap ? createCompatibleImage(h, w) : createCompatibleImage(w, h);
        Graphics2D g = (Graphics2D) rotated.getGraphics();
        // g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        // RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        // RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        g.drawImage(image, at, null);
        g.dispose();
        return rotated;
    }

    public static BufferedImage createCompatibleImage(int w, int h) {
        return graphicsConfiguration.createCompatibleImage(w, h, Transparency.OPAQUE);
    }
}

Related

  1. rotate(BufferedImage bi, double rotateValue)
  2. rotate(BufferedImage bi, int degree)
  3. rotate(BufferedImage image, double theta, int anchorX, int anchorY)
  4. rotate(BufferedImage image, float theRadiansAngle)
  5. rotate(BufferedImage image, int degrees)
  6. rotate(BufferedImage img, int angle)