I want to create a MouseMotionListener in Java that implements the mouseDragged method so it only cares about diagonal mouse movement.
I tried calculating the slope of the mouse position by saving the old X/Y and comparing it to a new X/Y like so:
public void mouseDragged(MouseEvent e) {
int deltaX = e.getX() - oldX;
int deltaY = e.getY() - oldY;
if (deltaX != 0) {
int slope = deltaY / deltaX;
if (slope == 1 || slope == -1) {
draggedHookPoint.setDraggedPosition(e);
}
}
}
oldX = e.getX();
oldY = e.getY();
}
The issue I have with this is that oldX and oldY are always the same as the new values. I thought maybe saving the values at the end of the method would work. How should I be saving those values and is there a better way to do this?
EDIT: so the oldX and oldY are not always the same, and I changed the code to look for
slope == -1 || slope == 1
, but the problem is that it still accepts some mouse moments that don't seem to be diagonal.