# [java] Rotating a picture

## Recommended Posts

I am trying to make a top view picture of a guy rotate with the mouse so that when I move the mouse he is always facing it. Any idea how this would be done?

##### Share on other sites
just calculate the dot product between the pictures normal view direction and the direction from the center of the image to the mouse cursor, that will give you the angle then simply rotate the image accordingly.

##### Share on other sites
yea, im just not sure how to rotate an image at all though. I can find the angle just fine.

##### Share on other sites
You may want to take a look at the AffineTransform class

##### Share on other sites
ok, I looked up some stuff about the AffineTransform class and it seems to be what I need, however I can not find any site that shows the methods and how to use them t. A brief explaination of how this class works or a link to somewhere would be awsome. thanks a lot

##### Share on other sites
It's on the API.

Don't know of a tutorial for this, but the methods are much self explanatory... play with them and you will find out what you need.

Son Of Cain

##### Share on other sites
I looked up some stuff but I am still not getting where I want to be. I tried doing it by just using:

g2.rotate(angle, x,y); //(where g2 = Graphics2D)

that made the picture rotate but so did everything else on the screen.

I tried finding some info on the whole AffineTransform thing. I tried using something like :

AffineTransform x1 = new AffineTransform();x1.rotate(angle, player.getX(), player.getY());g2.drawImage(player.getImage(), x1,null);

that made it rotate around the point 0,0 on the screen.

I really have no clue what's going on with this whole rotation thing, If anyone has a short sample of code or something that shows an image being rotated that would be the best way for me to understand it. Thanks

##### Share on other sites
Seems like that approach should work. But, try a different approach like this:

AffineTransform trans = new AffineTransform();trans.translate(player.getX(), player.getY());trans.rotate(angle);g2.drawImage(player.getImage(), trans, null);

Or if that isn't the right order, reverse the translate and the rotate.

##### Share on other sites
Optus- I like that a lot better. Thanks man

##### Share on other sites
Ok, I have the image rotating and everything. Now some wierd things are happening. I think I know what is happening but I am just not sure how to fix it. When the image rotates it does not update the player's X and Y position. The image will rotate but if I do a player.getX() or getY() it shows me the old X Y positions. I cannot get the player's X and Y to update to the new location of the image. Is there a way that I can access the image properties to obtain the new location?

Also, is there a way to change the point that the image rotates around? When I have the image flip 180 degrees it looks wierd because it rotates around the top left corner of the image, so when the image flips the top left corner stays in the same spot and the rest rotates around it. So after the rotate the image is higher up than it should be. I hope that makes sense. I think to fix this I would need the image to rotate around a center point in the image. Any ideas?

##### Share on other sites
g2 = (Graphics2D) g;

AffineTransform orig = g2.getTransform();

AffineTransform at = new AffineTransform();

at.rotate(angle,x_pos,y_pos);
g2.transform(at);
g2.drawImage (image,x_pos,y_pos, x_size, y_size, null);
g2.setTransform(orig);

This is the code I use to rotate images in a game im working on. Your right in the regard that you want to rotate the image around the center, to find the center just compute its width and height. The center will be x_pos + width/2 and the same for the y_pos. (above I already have x_pos and y_pos computed to be the center of my image).

The orig transform is set because once you rotate the image it will rotate the entire graphics, which is bad because everything else will rotate with it. So after you draw your rotated image on the screen, you set your transform back to the original.

If you want the new x and y positions of your image, you will have to use some trig to figure those out with the angle that you rotated the image. The easiest thing to do is just set your X and Y to be the center of your image, and then it eliminates many problems.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627638
• Total Posts
2978327

• 10
• 12
• 22
• 13
• 34