Java BufferedImage Rotate rotateFortyFiveClockwise(BufferedImage img)

Here you can find the source of rotateFortyFiveClockwise(BufferedImage img)

Description

Rotate img 45 degrees clockwise.

License

Open Source License

Parameter

Parameter Description
img The BufferedImage to rotate.

Return

The given image rotate 45 degrees clockwise.

Declaration

public static BufferedImage rotateFortyFiveClockwise(BufferedImage img) 

Method Source Code


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

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;

public class Main {
    /**// w ww  .  ja  v  a 2s  .  c om
     * Rotate img 45 degrees clockwise.
     *
     * @param img The BufferedImage to rotate.
     * @return The given image rotate 45 degrees clockwise.
     */
    public static BufferedImage rotateFortyFiveClockwise(BufferedImage img) {
        double piOverFour = Math.PI / 4.0;

        // the diagonal of the square becomes the new dimensions
        int w = img.getWidth();
        int h = img.getHeight();
        int diag = (int) Math.sqrt(w * w + h * h) + 2; // padding for good measure

        BufferedImage rotated = new BufferedImage(diag, diag, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = (Graphics2D) rotated.getGraphics();
        AffineTransform trans = g2d.getTransform();

        // the image is rotate about the top-left corner
        // so move it to the top center and rotate
        trans.translate(diag / 2, 0);
        trans.rotate(piOverFour);
        g2d.drawImage(img, trans, null);

        return rotated;
    }
}

Related

  1. rotate90(BufferedImage img, boolean left)
  2. rotate90ToRight(BufferedImage inputImage)
  3. rotateAndFlipSwappingRowsAndColumns(BufferedImage srcImage)
  4. rotateBufferedImage(BufferedImage bufferedImage, double angle)
  5. rotateByRightAngle(BufferedImage source, int degrees)
  6. rotateImage(BufferedImage image, double angle)
  7. rotateImage(BufferedImage image, double angle)
  8. rotateImage(BufferedImage image, double radians)
  9. rotateImage(BufferedImage image, float angle)