Search code examples
java2d-games

Getting the DX/DY of a mouse in Java


I'm using Java to create a simple game in 2D.

However, I would like to get the DX of the mouse so that i can move a certain object to a different place with my mouse in my game.

When i was learning a bit of LWJGL, there was a method called

Mouse.getDX()/~.getDY() 

It returns the movement on the x/y axis since last time getDY() was called.

But I'm not sure how to get such value without using any other libraries like LWJGL. I only know how to get the Mouse Position using the MouseListener interface. Or is there anything I've done wrong? Thanks if you can answer :)


Solution

  • See this StackOverflow: Get Mouse Position

    It gives information on how to get the mouse position and gives links to the java API for more details.

    Or, use this tutorial to write your own listener, storing the last known position to obtain the delta: https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html