[java] Rotating a picture

Started by
9 comments, last by c4nux0r 18 years, 9 months ago
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?
Advertisement
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.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
yea, im just not sure how to rotate an image at all though. I can find the angle just fine.
You may want to take a look at the AffineTransform class
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
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
a.k.a javabeats at yahoo.ca
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

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.
Optus- I like that a lot better. Thanks man
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?

This topic is closed to new replies.

Advertisement