How to make an Image rotate?

Hey ,
How can I make an image rotate?I want to make a rotation animation.
This is that I have tried but that doesnt work…


	public void render(Graphics g) {
		AffineTransform at = new AffineTransform();
		Graphics2D g2d = (Graphics2D) g;

		if (type == 1) {
			switch (bodypart) {
			case 1:
				g2d.drawImage(tex.exbodypart1, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart1.getWidth() / 2.0, tex.exbodypart1
					        .getHeight() / 2.0);
				break;
			case 2:
				g2d.drawImage(tex.exbodypart2, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart2.getWidth() / 2.0, tex.exbodypart2
					        .getHeight() / 2.0);
				break;
			case 3:
				g2d.drawImage(tex.exbodypart3, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart3.getWidth() / 2.0, tex.exbodypart3
					        .getHeight() / 2.0);
				break;
			case 4:
				g2d.drawImage(tex.exbodypart4, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart4.getWidth() / 2.0, tex.exbodypart4
					        .getHeight() / 2.0);
				break;
			case 5:
				g2d.drawImage(tex.exbodypart5, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart5.getWidth() / 2.0, tex.exbodypart5
					        .getHeight() / 2.0);
				break;
			case 6:
				g2d.drawImage(tex.exbodypart6, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart6.getWidth() / 2.0, tex.exbodypart6
					        .getHeight() / 2.0);
				break;
			case 7:
				g2d.drawImage(tex.exbodypart7, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart7.getWidth() / 2.0, tex.exbodypart7
					        .getHeight() / 2.0);
				break;
			case 8:
				g2d.drawImage(tex.exbodypart8, (int) x, (int) y, null);
				   at.rotate(45.0 * Math.PI / 180.0, tex.exbodypart8.getWidth() / 2.0, tex.exbodypart8
					        .getHeight() / 2.0);
				break;
			}
		}

	}

Thanks :slight_smile:

What have you tried? What did google tell you?

How are you drawing the image? Are you using Java2D, OpenGL, a library or framework?

I’m using java2d, I edited the topic.

Use an AffineTransformOp and pass it into the call to drawImage: http://stackoverflow.com/questions/8639567/java-rotating-images

Edit: There are a bunch of different ways to do this. The above link was just the first result for googling “java rotate image”.

I think that you also have to call [icode]g2d.setTransform(at);[/icode] right after rotating.

That was my question, 3 years ago ;D

You can use this method. It’s from my [icode]ImageTool[/icode] class.


/**
 * 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 (Image) bimg;
}

Hope this helps.

[quote=“Jimmt,post:6,topic:49762”]
Creepy. Even creepier is when you’re googling and start reading an answer and realize YOU wrote that but completely forgot.

Thats pretty complicated for such an easy task and I have tried it and it didnt work for me for some reason…

Works like a chram, thanks a lot!!
If someone is interested, this the working code:


	public void render(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		BufferedImage image = null;
		if (type == 1) {
			switch (bodypart) {
			case 1:
				image = tex.exbodypart1;
				break;
			case 2:
				image = tex.exbodypart2;
				break;
			case 3:
				image = tex.exbodypart3;
				break;
			case 4:
				image = tex.exbodypart4;
				break;
			case 5:
				image = tex.exbodypart5;
				break;
			case 6:
				image = tex.exbodypart6;
				break;
			case 7:
				image = tex.exbodypart7;
				break;
			case 8:
				image = tex.exbodypart8;
				break;
			}

			i += rotation;
			double rotationRequired = Math.toRadians(i);
			double locationX = image.getWidth(null) / 2;
			double locationY = image.getHeight(null) / 2;
			AffineTransform tx = AffineTransform.getRotateInstance(
					rotationRequired, locationX, locationY);
			AffineTransformOp op = new AffineTransformOp(tx,
					AffineTransformOp.TYPE_BILINEAR);
			g2d.drawImage(op.filter(image, null), (int) x, (int) y, null);

		}

	}