So far I have a java app where I draw a circle(player) and then draw a green rectangle on top(gun barrel). I have it so when the player moves, the barrel follows with it. I want it to find where the mouse is pointing and then rotate the barrel accordingly. For an example of what I mean look at this video I found http://www.youtube.com/watch?v=8W7WSkQq5SU See how the player image reacts when he moves the mouse around?
Here's an image of what the game looks like so far:
So how do I rotate it like this? Btw I don't like using affinetransform or Graphics2D rotation. I was hoping for a better way. Thanks
Using the
Graphics2D
rotation method is indeed the easiest way. Here's a simple implementation:If you want to remove the rotation when you're done so you can continue drawing normally, use:
It's a good idea to just use
Graphics2D
anyway for anti-aliasing, etc.Using
AffineTransform
, sorry, only way I know how :PWill produce this effect
The red line (point out from the center) will want to follow the cursor.