P
P
Pavel Subbotin2021-01-29 23:32:41
Java
Pavel Subbotin, 2021-01-29 23:32:41

How to rotate BufferedImage?

Guys how to rotate a BufferedImage? That is, I was able to rotate it, but it doesn’t look the way I would like, how can I fix it?

Here's how the image looks before the rotation:
601470844c6bc373174348.png
And here's how it looks after:
60147098f3540872878266.png

Code:

@Override
            public void actionPerformed(ActionEvent e) {
                Image img = image.getScaledInstance(image.getWidth(), image.getHeight(), Image.SCALE_DEFAULT);
                image = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
                Graphics2D g2 = image.createGraphics();
                //g2.translate(workSpace.getSize().width/2, workSpace.getSize().height/2);
                g2.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2);
                workSpace.setSize(image.getWidth(), image.getHeight());
                g2.drawImage(img, 0, 0, workSpace);
                workSpace.repaint();
                g2.dispose();
            }


I don't know what exactly the problem is, but it seems to me that the problem lies in the workSpace (the panel on which the BufferImage is located).

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question