It's not a secret anymore!
The moose likes Swing / AWT / SWT and the fly likes MouseDragged event issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseDragged event issue" Watch "MouseDragged event issue" New topic

MouseDragged event issue

Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

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.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46375
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.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20275

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.

How To Ask Questions How To Answer Questions
I agree. Here's the link:
subject: MouseDragged event issue
It's not a secret anymore!