# rotating a image from a given mouse coordinate

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

## Recommended Posts

Hi, Im playing about with java, and doing some simple sprite manipulation, but my math is not very good and so i was wondering if someone could explain to me how i would rotate and image to face the direction of where the mouse pointer is, the method call to rotate the image is easy enough but im unsure how i would work out how many degree it would need to turn, in order to face the mouse, hope im making sense? cheers

##### Share on other sites
this is simple trigonometry.
public void mouseMoved(MouseEvent e){    int mx=e.getX();    int my=e.getY();    int cx=imageX-imageWidth/2;    int cy=imageY-imageHeight/2;    int dx=mx-cx;    int dy=my-cy;    double angle=Math.atan2((double)dy,(double)dx);    Graphics2D g=(Graphics2D) YourFrame.getGraphics();    g.rotate(angle);    g.drawImage(image, imageX, imageY, null);}

##### Share on other sites
ah i see brilliant, thankyou
think i need to go read up on my trigonometry :)

cheers

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633325
• Total Posts
3011370
• ### Who's Online (See full list)

There are no registered users currently online

×