使用 AffineTransform 旋转动画 GIF (ImageIcon) [英] Rotate an animated GIF (ImageIcon) using AffineTransform

查看:27
本文介绍了使用 AffineTransform 旋转动画 GIF (ImageIcon)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 AffineTransform 旋转存储在 ImageIcon 中的动画 gif.结果是没有绘制图像.

I am trying to rotate an animated gif stored in an ImageIcon using an AffineTransform. The result is that the image does not get drawn.

这是我的代码:

AffineTransform trans = AffineTransform.getRotateInstance(imgYaw, img.getImage().getWidth(null) / 2, img.getImage().getHeight(null) / 2);
AffineTransformOp transo = new AffineTransformOp(trans, AffineTransformOp.TYPE_BILINEAR);
BufferedImage bufferedimg = new BufferedImage(img.getImage().getWidth(null), img.getImage().getHeight(null), BufferedImage.TYPE_4BYTE_ABGR);
img.setImage(atransO.filter(bufferedimg, null));
img.paintIcon(null, g, x, y);

推荐答案

与其说是答案,不如说是简化工作流程的示例...

Not so much an answer, but an example of simplified workflow...

基本上,它的作用是将 AffineTransform 直接应用于 Graphics 上下文并将 IconImage 绘制到它...

Basically, what this does is applies a AffineTransform directly to the Graphics context and paints the IconImage to it...

现在,如果您需要的话,您可以使用 BufferedImage 中的 Graphics 上下文...

Now, you could use the Graphics context from BufferedImage if that's what you need...

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestImage {

    public static void main(String[] args) {
        new TestImage();
    }

    public static final String IMAGE_PATH = "C:\\Users\\shane\\Dropbox\\Ponies\\28490 - animated gif rainbow_dash_Small.gif";

    public TestImage() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new GridLayout(1, 2));
                frame.add(new JLabel(new ImageIcon(IMAGE_PATH)));
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private ImageIcon img;
        private float degrees;

        public TestPane() {

            img = new ImageIcon(IMAGE_PATH);
            Timer timer = new Timer(16, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    degrees += 1;
                    repaint();
                }
            });
            timer.start();

        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(img.getIconWidth(), img.getIconHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            int x = getWidth() / 2;
            int y = getHeight() / 2;
            g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(degrees), x, y));
            x = (getWidth() - img.getIconWidth()) / 2;
            y = (getHeight() - img.getIconHeight()) / 2;
            img.paintIcon(this, g2d, x, y);
            g2d.dispose();
        }

    }

}

这篇关于使用 AffineTransform 旋转动画 GIF (ImageIcon)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆