Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MouseDragged event issue

 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49400
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20546
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic