Example usage for java.awt Graphics2D rotate

List of usage examples for java.awt Graphics2D rotate

Introduction

In this page you can find the example usage for java.awt Graphics2D rotate.

Prototype

public abstract void rotate(double theta, double x, double y);

Source Link

Document

Concatenates the current Graphics2D Transform with a translated rotation transform.

Usage

From source file:org.jahia.services.image.AbstractJava2DImageService.java

public boolean rotateImage(Image image, File outputFile, boolean clockwise) throws IOException {
    BufferedImage originalImage = ((BufferImage) image).getOriginalImage();

    BufferedImage dest = getDestImage(originalImage.getHeight(), originalImage.getWidth(), originalImage);
    // Paint source image into the destination, scaling as needed
    Graphics2D graphics2D = getGraphics2D(dest, OperationType.ROTATE);

    double angle = Math.toRadians(clockwise ? 90 : -90);
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = originalImage.getWidth(), h = originalImage.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    graphics2D.translate((neww - w) / 2, (newh - h) / 2);
    graphics2D.rotate(angle, w / (double) 2, h / (double) 2);
    graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
    if (originalImage.getColorModel() instanceof IndexColorModel) {
        graphics2D.drawImage(originalImage, 0, 0, graphics2D.getBackground(), null);
    } else {/*from   www .j ava2s.c  o  m*/
        graphics2D.drawImage(originalImage, 0, 0, null);
    }

    // Save destination image
    saveImageToFile(dest, ((BufferImage) image).getMimeType(), outputFile);
    return true;
}

From source file:org.uiautomation.ios.utils.InstrumentsGeneratedImage.java

private BufferedImage rotate() {
    int rotateDegrees = orientation.getRotationInDegree();
    boolean flip = orientation == Orientation.LANDSCAPE
            || orientation == Orientation.UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT;
    try {//from w  w  w  .  j ava 2  s . co m
        final BufferedImage originalImage = ImageIO.read(source);

        // no rotation needed.
        if (rotateDegrees == 0) {
            return originalImage;
        }

        // need to rotate.
        final BufferedImage rotated;
        int width;
        int height;
        if (flip) {
            width = originalImage.getHeight();
            height = originalImage.getWidth();
        } else {
            width = originalImage.getWidth();
            height = originalImage.getHeight();
        }
        rotated = new BufferedImage(width, height, originalImage.getType());

        // Rotate the image and then move it back up to the origin through a translation call, since it'll pivot around
        // the center point which will cause non-square images to offset by the different in height and width.
        final Graphics2D graphics = rotated.createGraphics();
        graphics.rotate(Math.toRadians(rotateDegrees), rotated.getWidth() / 2, rotated.getHeight() / 2);
        graphics.translate((rotated.getWidth() - originalImage.getWidth()) / 2,
                (rotated.getHeight() - originalImage.getHeight()) / 2);
        graphics.drawImage(originalImage, 0, 0, originalImage.getWidth(), originalImage.getHeight(), null);
        return rotated;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:VASSAL.tools.imageop.OrthoRotateOpBitmapImpl.java

public BufferedImage eval() throws Exception {
    final BufferedImage src = sop.getImage(null);
    if (size == null)
        fixSize();/*from w  w w  .j  av a 2  s  .c  o  m*/

    // remain opaque if our parent image is
    final BufferedImage dst = ImageUtils.createCompatibleImage(size.width, size.height,
            src.getTransparency() != BufferedImage.OPAQUE);

    final Graphics2D g = dst.createGraphics();
    g.rotate(Math.PI / 2.0 * angle, src.getWidth() / 2.0, src.getHeight() / 2.0);
    g.drawImage(src, 0, 0, null);
    g.dispose();

    return dst;
}