• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MouseDragged event issue

 
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic