• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Parallel dragging

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello coders,


I'm experimenting with mouseListeners and I have an interesting problem (I think). I've searched the forum for similar posts but found none. Sorry if I missed any.

In my program I'm trying to move parts of shapes by dragging one of its points. I've already implemented the mousePressed() and mouseDragged() methods, and I can move single points. But I need to find out how many pixels the mouse pointer moved in X and Y and in which direction, so that I can apply the same vector to the other points.

In the mouseWheelEvent there is a method called getWheelRotation(), which returns the direction of the wheel. I need something alike for X and Y, instead of the actual new position getX() and getY() are returning.


Any thoughts? Thanks a bunch in advance,

O.
--
Sex is an emotion in motion.
-- Mae West
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Olaf Enulfson wrote:In my program I'm trying to move parts of shapes by dragging one of its points. I've already implemented the mousePressed() and mouseDragged() methods, and I can move single points. But I need to find out how many pixels the mouse pointer moved in X and Y and in which direction, so that I can apply the same vector to the other points.


One way is to simply have your Mouse input listener or mouse adapter save the location of the mouse on mousePressed method and store this in a variable local to this listener. Then it is trivial to calculate the change in x and y from this point in the mouseDragged method.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Olaf Enulfson wrote:I'm trying to move parts of shapes


Also, much will depend on the details of just what you are trying to do. If you are trying to move a complete shape, then you may wish to use the Shape interface and affinetransforms. Otherwise you could encapsulate your shape yourself, and give it a move method. For instance:

SimpleBox.java


DragBoxPanel.java
 
Olaf Enulfson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pete, you are incredible.

I came up with your first suggestion too. I feel kind of bad for posting the question so soon. I should have given it more thought first.
But your second post blew me away. You're in a league of your own.

Thanks man. I appreciate it.

O.
 
I love a good mentalist. And so does this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic