【问题标题】:Rotate gif file format image using java使用java旋转gif文件格式图像
【发布时间】:2013-12-30 19:51:10
【问题描述】:

我想将我的 gif 图像旋转 90 度并将其显示在一个框架中。我曾尝试使用 AffineTransform()AffineTransformOP() 内置类。它适用于非动画图像,但不适用于 gif 图像。

我还检查了链接 Rotate GIF images in java 及其评论,它从来没有帮助我解决我的问题。

所以,请帮助我。

【问题讨论】:

    标签: java image animation rotation gif


    【解决方案1】:

    你可以设置正确的affineTransform,但你必须小心!

    public static void main(String[] args) throws MalformedURLException {
    
    File file = new File("img/ani.gif");
        URL url = file.toURI().toURL();
        Icon icon = new ImageIcon(url);
        final AffineTransform rot = AffineTransform.getRotateInstance(0.2);
        JLabel label = new JLabel(icon){
    
            @Override
            public void paint(Graphics arg0) {
    
               Graphics2D gr = (Graphics2D) arg0;
               AffineTransform original = gr.getTransform();        
               gr.setTransform(rot);
               super.paint(arg0);
               gr.setTransform(original);
            }           
        };        
    
        JFrame f = new JFrame("Animation");
        f.getContentPane().add(label);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setVisible(true);
    }
    

    那么,这段代码有什么作用?

    在 Label 开始任何绘图之前,我们获取 Graphics 并设置 affinetrans (rot 0.2) 然后,图形有这个“设置”来绘制动画。 绘制后,我们返回“原始”仿射变换。 您应该也可以在其他组件上执行此操作...

    重要提示

    来自:http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html#setTransform%28java.awt.geom.AffineTransform%29

    警告:此方法绝不能用于在现有变换之上应用新的坐标变换,因为 Graphics2D 可能已经具有其他用途所需的变换,例如渲染 Swing 组件或应用缩放变换来调整用于打印机的分辨率。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-11
      • 2012-07-31
      • 2017-04-26
      • 2022-01-09
      • 2012-08-23
      • 1970-01-01
      相关资源
      最近更新 更多