This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseDragged event issue" Watch "MouseDragged event issue" New topic
Author

MouseDragged event issue

Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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
Sheriff

Joined: Oct 13, 2005
Posts: 37941
    
  22
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
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MouseDragged event issue
 
Similar Threads
MouseDragg
How to shrink/grow lines
Motion listener hangs while mouse button is pressed
Applet - Drag and Drop
Moving multiple ojects in Java2D