【问题标题】:Rotate an animated GIF (ImageIcon) using AffineTransform使用 AffineTransform 旋转动画 GIF (ImageIcon)
【发布时间】:2014-09-23 22:31:13
【问题描述】:

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

这是我的代码:

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);

【问题讨论】:

  • 为什么不直接将图像绘制到Graphics 上下文中,实际上,为什么不将转换应用到Graphics 上下文中呢?对于example
  • @MadProgrammer 因为如果我这样做,图像不会动画。
  • @MadProgrammer 如果您的意思是将paintIcon 替换为g.drawImage(transO.filter(bufferedimg, null), x - (img.getImage().getWidth(null) / 2), y - (img.getImage().getWidth(null) / 2), null);,那么图像甚至不会被绘制。
  • 我是正式的白痴。我忘了在缓冲图像上绘制图像。
  • 我有一种有趣的感觉,有一种方法可以做......但我可能正在考虑其他事情......

标签: java animated-gif image-rotation affinetransform


【解决方案1】:

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

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

现在,如果您需要的话,您可以使用来自 BufferedImageGraphics 上下文...

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();
        }

    }

}

【讨论】:

  • 我修好了(见我的评论),但无论如何我都会给你答案。
  • @user3697209 是的,这只是一种不同方法的演示;)
猜你喜欢
  • 2013-12-15
  • 2015-03-20
  • 2013-03-22
  • 1970-01-01
  • 2012-12-17
  • 1970-01-01
  • 2013-08-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多