0

I'm trying to implement a dragging algorithm for swing components where they can be clicked and dragged. However, it appears that my movements are in the correct directions, but extremely exaggerated. As in, a tiny bit of mouse movement causes a very disproportionate move in the shape.

Here is the code where I determine the movement:

(xStart and yStart are the coordinates where the mouse is first clicked to begin the drag)

The component has a move(int deltaX, int delatY) method which does just that. An example move method for a line component looks like this:

@Override
public void move(int dx, int dy) {
    this.x1 += dx;
    this.y1 += dy;
    this.x2 += dx;
    this.y2 += dy;
}

The move method is called like this, where e is a mouse drag event:

currentShape.move(e.getX() - (int) xStart, e.getY() - (int) yStart);
Bassinator
  • 1,682
  • 3
  • 23
  • 50
  • Well, [that's one example](https://stackoverflow.com/questions/43904865/when-i-mouse-drag-to-draw-a-circle-the-shape-moves-in-certain-situations/43946668#43946668), [that's another](https://stackoverflow.com/questions/26476645/resizing-path2d-circle-by-clicking-and-dragging-its-outer-edge/26476909#26476909), [that's another possible example](https://stackoverflow.com/questions/26108351/getting-the-starting-x-and-y-coordinates-of-a-path2d-shape-drawn-on-jpanel/26112443#26112443) – MadProgrammer Nov 04 '17 at 02:49
  • The real question is. how are the details calculated? – MadProgrammer Nov 04 '17 at 02:50

1 Answers1

1

You want to have your dx dy values equal the change in the cursors movement from the last mouse event you received and not the change from where the shape was initially clicked on.

I would recommend making a keyframeX and keyframeY variable to store your last mouse event x and y values to compare against.

luckydog32
  • 909
  • 6
  • 13