public static BufferedImage rotate ( BufferedImage image , int angle ) { final int width = image . getWidth ( ) ; final int height = image . getHeight ( ) ; final int transparency = image . getColorModel ( ) . getTransparency ( ) ; final Rectangle rectangle = new Rectangle ( 0 , 0 , width , height ) ; rectangle . rotate ( angle ) ; final BufferedImage rotated = createImage ( rectangle . getWidth ( ) , rectangle . getHeight ( ) , transparency ) ; final Graphics2D g = rotated . createGraphics ( ) ; optimizeGraphicsSpeed ( g ) ; g . rotate ( Math . toRadians ( angle ) , rectangle . getWidth ( ) / 2.0 , rectangle . getHeight ( ) / 2.0 ) ; final double ox = rectangle . getWidth ( ) - ( double ) width ; final double oy = rectangle . getHeight ( ) - ( double ) height ; final double cos = UtilMath . cos ( angle ) ; final double sin = UtilMath . sin ( angle ) ; final double angleOffsetX = sin * ox + cos * oy ; final double angleOffsetY = - sin * oy + cos * ox ; g . drawImage ( image , null , ( int ) ( ( ox - angleOffsetX ) / 2.0 ) , ( int ) ( ( oy - angleOffsetY ) / 2.0 ) ) ; g . dispose ( ) ; return rotated ; }
Rotate an image with an angle in degree .