Introduction - Mouse Gestures
Mouse Gestures is a way to uniquely combine mouse clicks and subsequent mouse movements that would be interpreted as specific commands for the current application. A simple mouse gesture that you must be aware of is a mouse "drag", where you click on a particular object and move your mouse, keeping your mouse clicked, till a particular point. This might be interpreted by the application (or the OS), as a command, for example, to move the object from its source location to the location where the mouse was finally released.
Although, "dragging" is the most common type of mouse gesture, there are other types of mouse gestures. eg., in the Opera Browser, you can hold the right mouse button and move the mouse left to go to the previous page in the browser history.
Evidently, mouse gestures are cool, and lets your application users execute a command, without having them to take their hands out of the mouse into the keyboard.
This article explains how to implement such a mouse gesture in Java Swing, step by step. For more such tips and articles, click here.
Implementing Mouse Gestures in Java Swing
There are many Software libraries available in Java that will let you do this. The two most famous ones are iGesture and Smardec's Mouse Gestures.
In this article, let's discuss about one of these: the Smardec's "Mouse Gestures".
What Should You Be Able to Recognize?
To implement mouse gestures, the minimum you should be able to recognize are these:
While holding one of the mouse keys down(left, right or center), the user
- Moves the mouse down (Let's call it operation 'D')
- Moves the mouse up(Operation 'U')
- Moves the moue right (Operation 'R')
- Mouse the mouse left (Operation 'L')
Once your library allows you to do these, you can use a series of them to make your one command. For eg., a set of commands "DR"(down and then right), can be a command, that makes the user draw a command that looks like the alphabet 'L'.
Getting Practical: Recognizing the Mouse Gesture
Ok, enough theory. Let's write code. But before that, let's settle on a gesture that we need to track. Our simple application will be a blank form, with a label, where the user can make mouse gestures. When he makes a gesture as below, while holding the right mouse button we interpret it as a command, and show a message in the label.
Clearly, this command occurs in the order D,R,U while holding the right mouse button.
The Application
We will define how our application will look here.
- Initially, our application will look like this:
- When the user is in the middle of doing a 'DRU' (for drawing a 'U' on the screen):
- When the user does something wrong:
- When the user has successfully completed making a 'U':
The Code
Step 1: Get the Jar
- Download the "Mouse Gestures" jar file from here
- Unzip it and add the dist/mousegestures-1.2.jar file to your classpath
Step 2: Write the Code
Create an instance of the MouseGestures
class:
MouseGestures mouseGestures = new MouseGestures();
Make sure that you listen to the right mouse button:
mouseGestures.setMouseButton(MouseEvent.BUTTON3_MASK);
Similarly, You can use BUTTON1_MASK
, BUTTON2_MASK
for the center and left mouse buttons
Add a MouseGesturesListener
and write your code. See comments in the code for explanation.
Add the mousegesture
listener, that has code that process displays appropriate commands:
mouseGestures.addMouseGesturesListener(new MouseGesturesListener() {
Override the gestureMovementRecognized
and write your code. The currentGesture
will be a string
representing the short abbreviation of the current set of mouse gestures. For example, if the current user gesture is down and then right, then the currentGesture
will have DR
.
public void gestureMovementRecognized(String currentGesture) {
if("DRU".equals(currentGesture)){
label.setText(" " + currentGesture + " -
Wow, U have drawn 'U'");
}
else if("DRU".startsWith(currentGesture)){
label.setText(" " + currentGesture + " -
You need to make a DRU");
}
else{
label.setText(" " + currentGesture + " -
Wrong gesture! release your mouse and try again");
}
}
public void processGesture(String gesture) {
try {
Thread.sleep(400);
} catch (InterruptedException e) {}
label.setText(text);
}
});
Start the mouseGesture
:
mouseGestures.start();
References
History
- 29th June, 2008: Initial post