# [java] Rotating a picture

This topic is 4877 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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?

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633300
• Total Posts
3011266
• ### Who's Online (See full list)

There are no registered users currently online

×