10

I have managed to rotate an image 180 degrees but wish to rotate it 90 degrees clockwise can someone edit my code so that it does this with explanation. Thanks.

 private void rotateClockwise()
    {
        if(currentImage != null){
            int width = currentImage.getWidth();
            int height = currentImage.getHeight();
            OFImage newImage = new OFImage(width, height);
            for(int y = 0; y < height; y++) {
                for(int x = 0; x < width; x++) {
                    newImage.setPixel( x, height-y-1, currentImage.getPixel(x, y));
                }
        }
            currentImage = newImage;
            imagePanel.setImage(currentImage);
            frame.pack();
    }
    }
Vignesh Vino
  • 1,242
  • 4
  • 25
  • 50
Ryan Gibson
  • 283
  • 2
  • 3
  • 9
  • 1
    Try to think what happens with each quarter of the image when you rotate it. I think that should be one good approach. Solve each quarter individually. – Renato Lochetti Apr 10 '13 at 13:24
  • 3
    [example](http://forum.codecall.net/topic/69182-java-image-rotation/) – Vignesh Vino Apr 10 '13 at 13:24
  • Thanks for the example Vignesh Vino. I managed to get it working much appreciated. – Ryan Gibson Apr 10 '13 at 13:48
  • You need to swap x,y. I'd put this as an answer but can't. OFImage newImage = new OFImage(height, width); newImage.setPixel(height-1-y, x, currentImage.getPixel(x, y)); –  Aug 22 '15 at 18:42

2 Answers2

13

Use this method.

/**
 * Rotates an image. Actually rotates a new copy of the image.
 * 
 * @param img The image to be rotated
 * @param angle The angle in degrees
 * @return The rotated image
 */
public static Image rotate(Image img, double angle)
{
    double sin = Math.abs(Math.sin(Math.toRadians(angle))),
           cos = Math.abs(Math.cos(Math.toRadians(angle)));

    int w = img.getWidth(null), h = img.getHeight(null);

    int neww = (int) Math.floor(w*cos + h*sin),
        newh = (int) Math.floor(h*cos + w*sin);

    BufferedImage bimg = toBufferedImage(getEmptyImage(neww, newh));
    Graphics2D g = bimg.createGraphics();

    g.translate((neww-w)/2, (newh-h)/2);
    g.rotate(Math.toRadians(angle), w/2, h/2);
    g.drawRenderedImage(toBufferedImage(img), null);
    g.dispose();

    return toImage(bimg);
}

taken from my ImageTool class.

Hope it helps.

Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91
  • why did you copy the image , does it not accomplish by applying 90 degree rotated affine transform while drawing ? please clarify. – Mihir Aug 11 '13 at 07:16
  • @Mihir That's a requirement for my game. This class is from my game engine so I used that. You can use anything you like. – Sri Harsha Chilakapati Aug 12 '13 at 06:10
  • I actually had to create a new buffered image. Otherwise, I'm getting an unexpected results. – juminoz Apr 14 '14 at 17:00
  • As I understand it this code rotates clockwise but how do I rotate it counter clockwise? – Noosrep Nov 09 '15 at 12:55
  • @TimothyPersoon Use negative angles for anti clockwise rotation. – Sri Harsha Chilakapati Nov 09 '15 at 12:57
  • Damn, didn't expect it to be that simple. I have another issue though. I need to put a sleep of 9 seconds in the code because otherwise it looks like it doesn't clear it's cache rightaway. My image element is the width & height of the newly rotated dimensions but the image itself is not rotated, see http://imgur.com/cWr9WZc Do you have any idea what causes this? – Noosrep Nov 09 '15 at 13:06
  • @TimothyPersoon Search for existing questions or create a new question. This is not the place to ask about the issue of caches. – Sri Harsha Chilakapati Nov 09 '15 at 13:10
  • Yeah I thought so but it is working with your code so I thought maybe you had the same problems. Caching is just a guess, but I will post a new question – Noosrep Nov 09 '15 at 13:13
  • You don't need to introduce transcendental functions to achieve rotaion. You can rotate simply by multiplication like in complex numbers. https://www.youtube.com/results?search_query=njwildberger+rotation – Lukas Nov 27 '16 at 14:13
  • @Lukas Can you explain in detail? I do know Quaternions are useful for representing rotations, and I do use them. However they are more useful in 3D space, and also in real-time calculations. For most 2D games, this is sufficient. – Sri Harsha Chilakapati Nov 29 '16 at 15:31
  • Why use the heavy trig stuff when the rotation is 90 degrees? A much simpler solution is found here: https://coderanch.com/t/485958/java/Rotating-buffered-image – Per Lindberg Feb 02 '17 at 12:41
  • @PerLindberg That's true, but the thing is that I've copied this from my project, and my requirement is to rotate at arbitrary angles. – Sri Harsha Chilakapati Feb 02 '17 at 16:09