![]() |
Languages »
Java »
General
Intermediate
License: The Code Project Open License (CPOL)
Implementing Mouse Gestures in Java Swing: Step by StepBy kaushik_sathupadiA step by step guide to implement Mouse Gestures based commands in your Swing applications |
Java, Java (JavaSE 6, J2SE 5, J2SE 4), AWT, Swing, Dev, Design
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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".
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
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'.
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.
We will define how our application will look here.
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");
}
}
//This method is called when the user releases the mouse button finally
//Just display the current message for a few milliseconds then
//redisplay the original text
public void processGesture(String gesture) {
try {
Thread.sleep(400);
} catch (InterruptedException e) {}
label.setText(text);
}
});
Start the mouseGesture:
mouseGestures.start();
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 29 Jun 2008 Editor: Deeksha Shenoy |
Copyright 2008 by kaushik_sathupadi Everything else Copyright © CodeProject, 1999-2009 Web17 | Advertise on the Code Project |