user3763169
user3763169

Reputation: 1

move an object into left and right in screen with moving mouse pointer in java programming

I am writing a game in java language,in this game we have a gun that want to move it into right and left,do this without clicking mouse,as the mouse pointer moves right the gun moves right and like this for the left side.how can I do this with using Mouse Listener ? thanks.

Upvotes: 0

Views: 759

Answers (1)

Epiglottal Axolotl
Epiglottal Axolotl

Reputation: 1048

What you should do here is make a MouseMotionListener and implement the method mouseMoved(MouseEvent e). On whatever gui component draws the gun (I'm assuming it extends java.awt.Component), call addMouseMotionListener() with the listener that you made. You can then call e.getX() and e.getY() in mouseMoved() to get the coordinates of the mouse and then pass them somehow to the gun object.

Upvotes: 1

Related Questions