2014-09-23 85 views
0

我想使用AffineTransform旋轉存儲在ImageIcon中的動畫gif。結果是圖像沒有被繪製。使用AffineTransform旋轉動畫GIF(ImageIcon)

這裏是我的代碼:

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

爲什麼不直接將圖象畫到'Graphics'背景下,其實,轉型爲何不適用於'Graphics'背景?對於[示例](http://stackoverflow.com/questions/20275424/rotating-image-with-affinetransform/20280225#20280225) – MadProgrammer 2014-09-23 22:34:54

+0

@MadProgrammer因爲如果我這樣做,圖像不會動畫。 – user3697209 2014-09-23 22:37:02

+0

@MadProgrammer如果您的意思是用'g.drawImage(transO.filter(bufferedimg,null),x - (img.getImage()。getWidth(null)/ 2),y - (img.getImage()。getWidth (null)/ 2),null);',那麼圖像甚至不會被繪製。 – user3697209 2014-09-23 22:38:43

回答

1

與其說是一個答案,但簡化工作流程的例子...

基本上,這樣做是直接施加AffineTransformGraphics背景和油漆在IconImage它...

現在,你可以使用來自BufferedImageGraphics背景下,如果這就是你所需要的...

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

    } 

} 
+0

我修復了它(請參閱我的評論),但無論如何我會給你答案。 – user3697209 2014-09-24 00:33:10

+0

@ user3697209是的,這只是一個不同方法的演示;) – MadProgrammer 2014-09-24 00:59:49