This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am working on simple graphical editor, and have an issue with mouseDragged event of the drawing surface (JPanel).
For example, if I click on an element and drag it (let's say, ellipse) to move it. During the moving, I need to update my model, where I keep the list of elements drawn. But model is updated on "every pixel" my element is moved. The idea is to update model just once, only when element is dragged to new position and mouse button is released. So, it would be something like this:
while dragging calculate new coordinates
when mouse button is released, update model by setting new coordinates to element moved.
Is there a way to achieve this?
The quieter you are, the more you are able to hear.
Mouse button down = select coordinates
Mouse button released = select new coordinates and calculate difference.
Then you can move your shape, but it will move suddenly, rather than being dragged slowly.
There are two interfaces for mice: MouseListener and MouseMotionListener. You should find suitable methods in one or other of those interfaces.
Campbell Ritchie wrote:There are two interfaces for mice: MouseListener and MouseMotionListener.
Actually there are two more: MouseWheelListener for listening to mouse wheel movements, and MouseInputListener which simply extends both MouseListener and MouseMotionListener to combine the methods of both into one single interface. Combine this with the MouseInputAdapter skeleton implementation and you only need one interface and one class instead of four if you want to have both MouseListener and MouseMotionListener.