how to implement mouse gestures by tracking movement of mouse [closed]
I wonder how could I track the mouse movement (like scrolling) in Java, I need to detect whether a user has made a plus or m开发者_如何学Pythoninus sign. Anyone could help me about that?
to clarify:
I have a pdf file opened in a JFrame and a JPanel, when the mouse is in the panel`s bounds it is expected that he will make a plus(+) or minus(-) sign to zoom in and zoom out the pdf file.So i need to track the movement of the mouse.Thanks in advance
To track the motion of a mouse, you'll want to create a mouse motion listener:
public class MouseMotionEventDemo extends JPanel
implements MouseMotionListener {
//...in initialization code:
//Register for mouse events on blankArea and panel.
blankArea.addMouseMotionListener(this);
addMouseMotionListener(this);
...
}
public void mouseMoved(MouseEvent e) {
saySomething("Mouse moved", e);
}
public void mouseDragged(MouseEvent e) {
saySomething("Mouse dragged", e);
}
void saySomething(String eventDescription, MouseEvent e) {
textArea.append(eventDescription
+ " (" + e.getX() + "," + e.getY() + ")"
+ " detected on "
+ e.getComponent().getClass().getName()
+ newline);
}
}
The java documentation will help with this. To track when the user draws a plus or minus sign, I would recommend creating a grid. Each time the motion event fires, add the mouse's coordinates to the grid. When all of the coordinates match what you define as a "plus" sign or "minus" sign, you can do whatever you need to do afterwards.
Assuming you are using AWT or Swing, in principle, you need a MouseMotionListener. Implement it (or subclass MouseMotionAdapter and implement the relevant methods) and add it to the component where you want to observer the user.
You have to record the mouse positions somehow, and then do some signal recognition on it. There might be some libraries helping on this, but I don't know any.
精彩评论