Sign in to follow this  

rotating a image from a given mouse coordinate

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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 this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this