1

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.

Seephor
  • 1,692
  • 3
  • 28
  • 50
  • 1
    What kind of error margin are you willing to accept? It's unlikely that the mouse will move precisely vertically or horizontally without some movement in the other direction – MadProgrammer Dec 11 '15 at 21:20
  • @madprogrammer maybe.. off by 1. I just want to restrict the mouse motion logic to when it is moving close to a 45 degree line in any direction. – Seephor Dec 11 '15 at 21:25
  • 1
    Try and calculate the angle between the two points (old and new), that'd be easier – MadProgrammer Dec 11 '15 at 21:30

1 Answers1

0

I wrote up this code, basically you draw a line on the JFrame and it will tell you whether the line was straight or diagonal. If it was diagonal it will give you the slope between the start and end.

public class Test extends JFrame implements MouseMotionListener, MouseListener {

public boolean diagonal;

public Point start;

public static void main(String[] args) throws IOException {
    JFrame j = new Test();
}

public Test() {
    setSize(600, 600);
    setVisible(true);
    addMouseMotionListener(this);
    addMouseListener(this);
}

@Override
public void mouseDragged(MouseEvent arg0) {
    if (start == null) {
        start = new Point(arg0.getX(), arg0.getY());
    }
}

@Override
    public void mouseReleased(MouseEvent arg0) {
        if (start != null) {
            if (start.x != arg0.getX() && start.y != arg0.getY()) {
                System.out.println("Diagonal");
                double slope = (arg0.getY()-start.y)/(arg0.getX()-start.getX());
                System.out.println("Slope is: " + slope);
            } else {
                System.out.println("Straight");
            }
            start = null;
        }

    }
@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

}

Andre
  • 778
  • 1
  • 5
  • 23