create Rotated Image - Java 2D Graphics

Java examples for 2D Graphics:BufferedImage Create

Description

create Rotated Image

Demo Code

/*******************************************************************************
 * Copyright (c) 2008 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:// w  w  w  .  j a v a  2  s.c o m
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/
//package com.java2s;
import java.awt.Color;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;

import java.awt.image.BufferedImage;

public class Main {
    private static BufferedImage createRotatedImage(Object src, int width,
            int height, int angle) {
        angle = angle % 360;

        if (angle < 0) {
            angle += 360;
        }

        if (angle == 0) {
            return renderRotatedObject(src, 0, width, height, 0, 0);
        } else if (angle == 90) {
            return renderRotatedObject(src, -Math.PI / 2, height, width,
                    -width, 0);
        } else if (angle == 180) {
            return renderRotatedObject(src, Math.PI, width, height, -width,
                    -height);
        } else if (angle == 270) {
            return renderRotatedObject(src, Math.PI / 2, height, width, 0,
                    -height);
        } else if (angle > 0 && angle < 90) {
            double angleInRadians = ((-angle * Math.PI) / 180.0);
            double cosTheta = Math.abs(Math.cos(angleInRadians));
            double sineTheta = Math.abs(Math.sin(angleInRadians));

            int dW = (int) (width * cosTheta + height * sineTheta);
            int dH = (int) (width * sineTheta + height * cosTheta);

            return renderRotatedObject(src, angleInRadians, dW, dH, -width
                    * sineTheta * sineTheta, width * sineTheta * cosTheta);

        } else if (angle > 90 && angle < 180) {
            double angleInRadians = ((-angle * Math.PI) / 180.0);
            double cosTheta = Math.abs(Math.cos(angleInRadians));
            double sineTheta = Math.abs(Math.sin(angleInRadians));

            int dW = (int) (width * cosTheta + height * sineTheta);
            int dH = (int) (width * sineTheta + height * cosTheta);

            return renderRotatedObject(src, angleInRadians, dW, dH,
                    -(width + height * sineTheta * cosTheta), -height / 2);

        } else if (angle > 180 && angle < 270) {
            double angleInRadians = ((-angle * Math.PI) / 180.0);
            double cosTheta = Math.abs(Math.cos(angleInRadians));
            double sineTheta = Math.abs(Math.sin(angleInRadians));

            int dW = (int) (width * cosTheta + height * sineTheta);
            int dH = (int) (width * sineTheta + height * cosTheta);

            return renderRotatedObject(src, angleInRadians, dW, dH, -(width
                    * cosTheta * cosTheta), -(height + width * cosTheta
                    * sineTheta));

        } else if (angle > 270 && angle < 360) {
            double angleInRadians = ((-angle * Math.PI) / 180.0);
            double cosTheta = Math.abs(Math.cos(angleInRadians));
            double sineTheta = Math.abs(Math.sin(angleInRadians));

            int dW = (int) (width * cosTheta + height * sineTheta);
            int dH = (int) (width * sineTheta + height * cosTheta);

            return renderRotatedObject(src, angleInRadians, dW, dH, (height
                    * cosTheta * sineTheta),
                    -(height * sineTheta * sineTheta));

        }

        return renderRotatedObject(src, 0, width, height, 0, 0);
    }

    private static BufferedImage renderRotatedObject(Object src,
            double angle, int width, int height, double tx, double ty) {
        BufferedImage dest = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_ARGB);

        Graphics2D g2d = (Graphics2D) dest.getGraphics();
        g2d.setColor(Color.black);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        AffineTransform at = AffineTransform.getRotateInstance(angle);
        at.translate(tx, ty);
        g2d.setTransform(at);

        if (src instanceof TextLayout) {
            TextLayout tl = (TextLayout) src;
            tl.draw(g2d, 0, tl.getAscent());
        } else if (src instanceof Image) {
            g2d.drawImage((Image) src, 0, 0, null);
        }
        g2d.dispose();

        return dest;
    }
}

Related Tutorials