0

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.

Here's my code:

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);
  • Why not draw the image directly to the `Graphics` context, in fact, why not apply the transformation to the `Graphics` context? For [example](http://stackoverflow.com/questions/20275424/rotating-image-with-affinetransform/20280225#20280225) – MadProgrammer Sep 23 '14 at 22:34
  • @MadProgrammer Because the image doesn't animate if I do that. – user3697209 Sep 23 '14 at 22:37
  • @MadProgrammer if you mean replacing paintIcon with `g.drawImage(transO.filter(bufferedimg, null), x - (img.getImage().getWidth(null) / 2), y - (img.getImage().getWidth(null) / 2), null);`, then the image doesn't even get drawn. – user3697209 Sep 23 '14 at 22:38
  • I am officially a **moron**. I forgot to draw the image on the bufferedimage. – user3697209 Sep 23 '14 at 22:55
  • I got a funny feeling that there is a way to do...but I might be thinking about something else... – MadProgrammer Sep 23 '14 at 23:39

1 Answers1

1

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

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

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

Rotating

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

    }

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366