Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Parallel dragging

 
Olaf Enulfson
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic