1

enter image description hereenter image description hereI am trying to rotate an image, somewhat it works but the problem is that it is not working properly. It's not rotating at exact I want. Image is displaying in some mix formation.

My code on button click :

RT90.addActionListener(new ActionListener() 
        {           
            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                degrees+=90;
                rotateIMG(degrees);
                repaint();
            }
        }); 

rotateIMG() code :

public void rotateIMG(double d)
    {
        BufferedImage b ;
        b=a;
        Graphics g;
        g=b.createGraphics();
        Graphics2D g2d = (Graphics2D)g;

        System.out.println(b.getWidth());
        System.out.println(b.getHeight());

        g2d.rotate(Math.toRadians(d), b.getWidth()/2, b.getHeight()/2);
        g2d.drawImage(b,0,0,null);

        ImageIcon rtimg = new ImageIcon(b);
        label.setIcon(rtimg);

    }

Any idea what's wrong in this code? Here a is buffered image which is loaded from image stack and label is JLabel using to display image.

Java Curious ღ
  • 3,622
  • 8
  • 39
  • 63

2 Answers2

2

You are overwriting the image you use as the source (b == a). You need to create a new instead.

public void rotateIMG(double d) {
    // Consider also using GraphicsConfiguration.createCompatibleImage().
    // I'm just putting here something that should work
    BufferedImage b = new BufferedImage(a.getHeight(), a.getWidth(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = b.createGraphics();

    g2d.rotate(Math.toRadians(d), a.getWidth()/2, a.getHeight()/2);
    // Note the a instead of b here
    g2d.drawImage(a, 0, 0, null);
    // Do you want to keep the old a or not?
    // a = b;
    ImageIcon rtimg = new ImageIcon(b);
    label.setIcon(rtimg);
}
kiheru
  • 6,588
  • 25
  • 31
  • sir, that code works but the problem is that some portion of image is croped means its not displayed after rotate. – Java Curious ღ Aug 16 '13 at 14:19
  • 1
    @user2659972 I just wrote the code, not tried it :-P. The problem may be that since the image dimensions changed the GUI needs to reflect that in the layout. I do not know how your layout works so I can not be sure. – kiheru Aug 16 '13 at 14:27
  • 1
    Another possible source is the translation used in the `rotate()` call. I did not check if it's correct, but copied it from you. – kiheru Aug 16 '13 at 14:29
  • ok i will try by myself if any problem i face then i will post and thank you for your support. – Java Curious ღ Aug 17 '13 at 05:59
  • 1
    The positioning can be a bit complicated. Take a look at the link posted by @camickr. – kiheru Aug 17 '13 at 07:09
  • yes i am trying that one. if i will made it then i will post you. – Java Curious ღ Aug 17 '13 at 07:45
2

the problem is that some portion of image is cropped

Check out Rotated Icon. It will calculate the correct size of the Icon as it is rotated at various degrees.

camickr
  • 321,443
  • 19
  • 166
  • 288