Java BufferedImage Operation tilt(BufferedImage image, double angle)

Here you can find the source of tilt(BufferedImage image, double angle)

Description

tilt

License

Open Source License

Declaration

public static BufferedImage tilt(BufferedImage image, double angle) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) JavaPEG developers/*from   w  w  w.  j  a  va  2 s.c o  m*/
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.awt.*;
import java.awt.image.BufferedImage;

public class Main {
    public static BufferedImage tilt(BufferedImage image, double angle) {
        double sin = Math.abs(Math.sin(angle));
        double cos = Math.abs(Math.cos(angle));

        int w = image.getWidth();
        int h = image.getHeight();

        int neww = (int) Math.floor(w * cos + h * sin);
        int newh = (int) Math.floor(h * cos + w * sin);

        GraphicsConfiguration gc = getDefaultConfiguration();
        BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
        Graphics2D g = result.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(angle, w / 2, h / 2);
        g.drawRenderedImage(image, null);
        g.dispose();
        return result;
    }

    public static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
}

Related

  1. testNeighbours(BufferedImage source, int x, int y, boolean alpha)
  2. texture2D(BufferedImage normalMap, int x, int y)
  3. thresholdImage(BufferedImage image, int threshold)
  4. tile(BufferedImage source, Rectangle r, GraphicsConfiguration conf)
  5. tileStretchPaint(Graphics g, Component component, BufferedImage image, Insets insets)
  6. tintImage(BufferedImage src, Color color, float tintOpacity)
  7. unweaveFrom(BufferedImage bufferedImage)
  8. updateImageSpecifications( BufferedImage bufferedImage)
  9. validateSelectionRectangle(BufferedImage image, Rectangle selection)