当前位置: 首页 > 知识库问答 >
问题:

如何在Java Swing中将ImageIcon旋转一定的度数?

傅阿苏
2023-03-14

我需要将ImageIcon旋转到Java中的缓冲图像。我已经尝试了所有可能的方法,是否有任何方法,我已经尝试将ImageIcon转换为bufferedImage。

我尝试了所有可能的StackOverflow解决方案

共有1个答案

郏扬
2023-03-14
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

public class Test {

    public static void main(String[] args) {
        // Create GUI
        GUI gui = new GUI();

        // Schedule task; rotate img every 1s
        Timer timer = new Timer(1000, e -> gui.rotateImg());
        timer.setRepeats(true);
        timer.start();
    }

    static class GUI extends JFrame {
        // Web url for image of cute doggo
        private static final String IMAGE_URL = "https://i.pinimg.com/736x/10/b2/6b/10b26b498bc3fcf55c752c4e6d9bfff7.jpg";

        // Cache image and UI components for rotation
        private BufferedImage image;
        private ImageIcon icon;
        private JLabel label;

        // Create new JFrame
        public GUI() {
            // Config grame
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(700, 700);
            setLocationRelativeTo(null);


            URL url;
            image = null;

            try {
                // Download + cache image from web
                url = new URL(IMAGE_URL);
                image = ImageIO.read(url);
            } catch (IOException e) {
                // Handle error downloading
                System.out.println("Failed to read image due to: " + e.getMessage());
            } finally {
                // On success - create/cache UI components
                if (image != null) {
                    // In this example I am using a label here to display an ImageIcon
                    // But at root the ImageIcon is holding a BufferedImage which is what we're modifying on rotation
                    add(label = new JLabel(icon = new ImageIcon(image)));
                }
            }

            // Show configured JFrame
            setVisible(true);
        }

        public void rotateImg() {
            if (image == null) return;

            // Rotate image
            BufferedImage rotated = rotateImg(image, Math.toRadians(90));
            image = rotated;

            // Add rotated image on ui thread
            SwingUtilities.invokeLater(() -> {
                icon.setImage(rotated);
                label.updateUI();
            });
        }

        // SRC: https://www.delftstack.com/howto/java/java-rotate-image/
        private BufferedImage rotateImg(BufferedImage img, double degrees) {
            int w = img.getWidth(), h = img.getHeight();

            BufferedImage imgCopy = new BufferedImage(w, h, img.getType());
            Graphics2D g2d = imgCopy.createGraphics();

            g2d.rotate(degrees, w / 2, h / 2);
            g2d.drawImage(img, null, 0, 0);

            return imgCopy;
        }
    }
}
 类似资料:
  • 这是我当前的目录结构(包含在目录中): 基本上来自,我正在尝试访问。 我尝试了很多变体来工作,现在我只有: 我尝试改变路径使用,使用函数,但似乎没有工作。 当我使用生成jar并运行jar文件时,它不会显示图像。 我执行

  • 问题内容: 首先,这是我使用秋千的第一个星期,如果我的问题太明显,那就对不起。另外,我需要使用标准Java库的解决方案,因为这是用于家庭作业,不允许使用奇怪的库。 我将JLabel与ImageIcon一起使用以在JFrame上显示图像。现在,我想将屏幕上的图像旋转到任意角度。我发现了有关Graphics2D的一些信息,但我找不到实现此目的的方法。 由于我发现的解决方案不起作用或不理解,因此我对旋转

  • 但似乎什么都没起作用。

  • 问题内容: 我正在尝试使用以下代码将文本旋转到270度: 它在所有浏览器中都能正常工作,但在IE8中却不能。我究竟做错了什么? 问题答案: 尝试使用此:

  • 我写了一个函数,它接受两个参数: > JPG图像作为3D数组 α给出的旋转度我的方法是: 公共静态整数[][]旋转(整数[][]img,双alpha){双rad=Math.toRadians(alpha);双sin=Math.sin(rad);双cos=Math.cos(rad); } 在固定索引范围的同时,输出是一个黑色的图像。我错过了什么?