Java Reference
In-Depth Information
}}} );
...
}
The mouseMoved method is called when the mouse is moved without a mouse button
being pressed. The mouseDragged method is called when the mouse is moved while one or
more buttons are being pressed. Note that we should not use the code e.getButton() in
this case because more than one mouse button could potentially be pressed. Instead, when
writing code for the mouseDragged method, we should always call the getModifiersEx
method. It will return code that shows us which mouse buttons are pressed.
Note that the behavior of the getModifiersEx method is a little tricky to understand.
If we do an experiment, we will see that method returns 1024 when the left mouse button is
pressed, 2048 when the middle mouse button is pressed, 4096 when the right mouse button
is pressed, 3072 when both the left and middle button are pressed and so on. In other words,
Java will return a unique integer for every mouse-button combination. We may be tempted
to just enumerate all combinations when the left mouse button is pressed. However, this is
the wrong approach because there is no guarantee that the getModifiersEx method will
not return different values in the next Java version.
Let us look in greater detail at the codes that are returned by the getModifiersEx
method. If the left mouse button is pressed, then the number 1024 is returned, which is the
binary number 0000 0100 0000 0000. If the middle button is pressed, then the binary number
0000 1000 0000 0000 is returned. If both the left and middle button are pressed, then the
number 0000 1100 0000 0000 is returned. In other words, there is a bitmap mask for each
mouse button. If more than one button is pressed, then the combination of the bitmap masks
of the buttons is returned. If we try to print the constant MouseEvent.BUTTON1 DOWN MASK ,
then we will get the binary number 0000 0100 0000 0000. This corresponds to just the left
mouse button being pressed. The operation “&” is the bitwise and operator. It performs
the operation and on each pair of bits. The result is 1 only when both bits are one. In other
words, we can think of 1 as true and of 0 as false . Suppose that we want to evaluate
the result of the expression e.getModifiersEx() & MouseEvent.BUTTON1 DOWN MASK .If
e.getModifiersEx() returns 0000 1100 0000 0000 and MouseEvent.BUTTON1 DOWN MASK is
equal to 0000 0100 0000 0000, then we will get the following result.
0000 1100 0000 0000
0000 0100 0000 0000
−−−−−−−−−−−−−−−−−−−−−−−−
(result of &)
0000 0100 0000 0000
In other words, the result is different from 0, which means that the left mouse button is
pressed. Alternatively, if we want to check if the left or right mouse button is pressed, then
we can write the following code.
e. getModifiersEx() & (MouseEvent.BUTTON1DOWN MASK | MouseEvent .
BUTTON2 DOWN MASK)
” is the bitwise or operator. The resulting bit is 1 if one of the
input bits is 1. In the above expression, the right-hand side of the operator & will create a
number that has two 1s in its binary representation. If the left-hand side has one of these 1s
set (i.e., the left or middle mouse button is pressed), then the whole expression will evaluate
to a number that is different from 0.
On a side note, one might be tempted to use & instead of && and
Note that the operator “
|
.
This is certainly possible. However, the operations are not equivalent. For example, the &&
operation will not evaluate the second argument if the first argument is false . Conversely,
the & operation always evaluates both arguments.
|
instead of
||
 
Search WWH ::




Custom Search