Interface Bild rotieren

CptK

Bekanntes Mitglied
Hallo, ich habe folgende Methode aus dem Internet, um ein Bild zu rotieren:
Java:
//Methode zum Rotieren eines Bildes
    public BufferedImage rotateImageX(BufferedImage img, double angle) {
        double sin = Math.abs(Math.sin(Math.toRadians(angle)));
        double cos = Math.abs(Math.cos(Math.toRadians(angle)));
        int w = img.getWidth(null), h = img.getHeight(null);
        int neww = (int) Math.floor(w * cos + h * sin);
        int    newh = (int) Math.floor(h * cos + w * sin);
        BufferedImage bimg = new BufferedImage(neww, newh,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bimg.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.drawRenderedImage(img, null);
        g.dispose();
        return bimg;
    }

Ich bekomme auch ein rotiertes Bild heraus, das Problem ist jetzt erstens, dass das Bild verzerrt wird und zweitens, dass das Bild nicht um seinen Mittelpunkt rotiert wird.
Wäre schön, wenn mir wer sagen könnte, wie ich das Bild rotieren muss, dass es sich nicht verzieht und sich um seinen eigenen Mittelpunkt dreht.

Also z.B. ich habe ein Bild mit 120px * 70px, das bei 100px und 100px gezeichnet wird. die Länge und Höhe sollen sich nicht ändern und in diesem Fall soll es sich um den Punkt (x= 160| y = 135) drehen.

Vielen Dank!
 
Er wollte ja nicht, dass du die komplette Powerpoint-Präsentation liest sondern das Zitat. Der Link ist nur die Quellenangabe.
 
Soll das jetzt so aussehen, weil das geht immer noch nicht....
Code:
//Methode zum Rotieren eines Bildes
    public BufferedImage rotateImageX(BufferedImage img, double angle) {
        double sin = Math.abs(Math.sin(Math.toRadians(angle)));
        double cos = Math.abs(Math.cos(Math.toRadians(angle)));
        int w = img.getWidth(null), h = img.getHeight(null);
        int neww = (int) Math.floor(w * cos + h * sin);
        int    newh = (int) Math.floor(h * cos + w * sin);
        BufferedImage bimg = new BufferedImage(neww, newh,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bimg.createGraphics();
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.translate((neww - w) / 2, (newh - h) / 2);
        g.drawRenderedImage(img, null);
        g.dispose();
        return bimg;
    }
 
Dieses Programm dreht ein Bild und zeigt es an.
Java:
import java.awt.*;
import java.awt.image.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

class Test extends JPanel {

    private static BufferedImage img;

    public Test() {
        img = rotateImageX(img, 45);
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(img, 0, 0, this);
    }

    // Methode zum Rotieren eines Bildes
    public BufferedImage rotateImageX(BufferedImage img, double angle) {
        double sin = Math.abs(Math.sin(Math.toRadians(angle)));
        double cos = Math.abs(Math.cos(Math.toRadians(angle)));
        int w = img.getWidth(null), h = img.getHeight(null);
        int neww = (int) Math.floor(w * cos + h * sin);
        int newh = (int) Math.floor(h * cos + w * sin);
        BufferedImage bimg = new BufferedImage(neww, newh, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = bimg.createGraphics();
        g.rotate(Math.toRadians(angle), w / 2, h / 2);
        g.drawRenderedImage(img, null);
        g.dispose();
        return bimg;
    }

    public static void main(String[] args) throws IOException {

        img = ImageIO.read(ClassLoader.getSystemResource("meinbildddd.jpg"));

        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(img.getWidth() + 15, img.getHeight() + 40);
        f.add(new Test());
        f.setVisible(true);
    }

}
 
Zuletzt bearbeitet:
Die verwendete Methode Graphics2D.rotate() hat übrigens drei Parameter. in der Doku steht:
Code:
Concatenates the current Graphics2D Transform with a translated rotation transform.
Subsequent rendering is transformed by a transform which is constructed by translating to the specified location, rotating by the specified radians, and translating back by the same amount as the original translation.
This is equivalent to the following sequence of calls:

         translate(x, y);
         rotate(theta);
         translate(-x, -y);
 
Dieses Programm dreht ein Bild und zeigt es an
Ich rieche verdächtig StackOverflow. 😉

@UnknownInnocent Drehung um 270 degrees gegen den Uhrzeigersinn:

Java:
void rot(String a, String b) throws IOException {
    BufferedImage c = ImageIO.read(new File(a));
    int d = 270;
    for (int x = 0; x < c.getWidth(); x++) {
        for (int y = 0; y < c.getHeight(); y++) {
            int e = (int) ((x-c.getWidth()/2f)*Math.cos(Math.toRadians(d))-(y-c.getHeight()/2f)*Math.sin(Math.toRadians(d))+c.getWidth()/2f);
            int f = (int) ((x-c.getWidth()/2f)*Math.sin(Math.toRadians(d))+(y-c.getHeight()/2f)*Math.cos(Math.toRadians(d))+c.getHeight()/2f-1);
            int g = c.getRGB(x, y);
            c.setRGB(x, y, c.getRGB(e, f));
            c.setRGB(e, f, g);
        }
    }
    ImageIO.write(c, "JPG", new File(b));
}

Das Bild hat danach nur ein paar Fehler 🙁
 
Dieses Programm dreht ein Bild und zeigt es an.
Vielen Dank für die Hilfe von euch allen, habe es jetzt hinbekommen. Mein Hauptfehler war, dass ich Vollidiot die Größen von dem Bild zum zeichnen benutzt habe, die ich am Anfang gespeichert hatte. Natürlich verzieht sich das Bild, wenn man nicht die Größen von dem neuen BufferedImage verwendet.....
 
Wenn Du ein quadratisches Bild nicht um 90, 180 oder 270 degrees drehst dann brauchst Du mehr Platz 😉 (länge breite vergrößern sich)
 

Zurück
Oben