render Rotated Object - Java 2D Graphics

Java examples for 2D Graphics:Transform

Description

render Rotated Object

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:/*from  w  w  w . j av  a2s .  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 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