0

I want to rotate my gif image with 90 degree and display it in a frame. I had tried using AffineTransform() and AffineTransformOP() inbuilt classes. Its working well for non animated image, but not for gif image.

I also check with the link Rotate GIF images in java and its comment, it never helping me to solve my problem.

So, plz help me.

Community
  • 1
  • 1
RAGHU
  • 3
  • 4

1 Answers1

0

you CAN set the right affineTransform, but you must be careful!

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

so, what does this code do?

before the Label start any drawing, we get the Graphics and set the affinetrans (rot 0.2) then, the graphic has this 'setting' to draw the animation. after drawing we return the 'original' affinetransform. you should be able to do it on other components as well...

IMPORTANT NOTE

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

WARNING: This method should never be used to apply a new coordinate transform on top of an existing transform because the Graphics2D might already have a transform that is needed for other purposes, such as rendering Swing components or applying a scaling transformation to adjust for the resolution of a printer.

Martin Frank
  • 3,445
  • 1
  • 27
  • 47