Mouse Events Core Java

You do not need to handle mouse events explicitly if you just want the user to be able to click on a button or menu. These mouse operations are handled internally by the various components in the user interface. However, if you want to enable the user to draw with the mouse, you will need to trap mouse move, click, and drag events. In this section, we show you a simple graphics editor application that allows the user to place, move, and erase squares on a canvas .

A mouse test program

A mouse test program

When the user clicks a mouse button, three listener methods are called: mouse Pressed when the mouse is first pressed, mouse Released when the mouse is released, and, finally, mouse -Clicked. If you are only interested in complete clicks, you can ignore the first two methods.

By using the getX and getY methods on the MouseEvent argument, you can obtain the xand y-coordinates of the mouse pointer when the mouse was clicked. To distinguish between single, double, and triple (!) clicks, use the getClickCount method.

Some user interface designers inflict mouse click and keyboard modifier combinations,such as CONTROL + SHIFT + CLICK, on their users. We find this practice reprehensible, but if you disagree, you will find that checking for mouse buttons and keyboard modifiers is a mess.

You use bit masks to test which modifiers have been set. In the original API, two of the button masks equal two keyboard modifier masks, namely

This was done so that users with a one-button mouse could simulate the other mouse buttons by holding down modifier keys instead. However, as of Java SE 1.4, a different approach is recommended. There are now masks

The getModifiersEx method accurately reports the mouse buttons and keyboard modifiers of a mouse event.

Note that BUTTON3_DOWN_MASK tests for the right (nonprimary) mouse button under Windows. For example, you can use code like this to detect whether the right mouse button is down:

In our sample program, we supply both a mousePressed and a mouseClicked method. When you click onto a pixel that is not inside any of the squares that have been drawn, a new square is added. We implemented this in the mousePressed method so that the user receives immediate feedback and does not have to wait until the mouse button is released. When a user double-clicks inside an existing square, it is erased. We implemented this in the mouse Clicked method because we need the click count.

As the mouse moves over a window, the window receives a steady stream of mouse movement events. Note that there are separate Mouse Listener and MouseMotionListener interfaces. This is done for efficiency —there are a lot of mouse events as the user moves the mouse around, and a listener that just cares about mouse clicks will not be bothered withunwanted mouse moves.

Our test application traps mouse motion events to change the cursor to a different shape (a cross hair) when it is over a square. This is done with the getPredefinedCursor method of the Cursor class. Table below lists the constants to use with this method along with what the cursors look like under Windows.

Sample Cursor Shapes

Sample Cursor Shapes

Here is the mouseMoved method of the Mouse Motion Listener in our example program:

NOTE: You can also define your own cursor types through the use of the create Custom Cursor method in the Toolkit class:

The first parameter of the create Custom Cursor points to the cursor image. The second parameter gives the offset of the “hot spot” of the cursor. The third parameter is a string that describes the cursor. This string can be used for accessibility support. For example, a screen reader program can read the cursor shape description to a user who is visually impaired or who simply is not facing the screen.

If the user presses a mouse button while the mouse is in motion, mouse Dragged calls are generated instead of mouseMoved calls. Our test application lets a user drag the square under the cursor. We simply update the currently dragged rectangle to be centered under the mouse position. Then, we repaint the canvas to show the new mouse position.

NOTE: The mouseMoved method is only called as long as the mouse stays inside the component. However, the mouseDragged method keeps getting called even when the mouse is being dragged outside the component.

There are two other mouse event methods: mouse Entered and mouse Exited. These methodsare called when the mouse enters or exits a component.

Finally, we explain how to listen to mouse events. Mouse clicks are reported through the mouseClicked procedure, which is part of the Mouse Listener interface. Because many applications are interested only in mouse clicks and not in mouse moves and because mouse move events occur so frequently, the mouse move and drag events are defined in a separate interface called Mouse Motion Listener.

In our program we are interested in both types of mouse events. We define two inner classes: Mouse Handler and Mouse Motion Handler. The Mouse Handler class extends the Mouse Adapter class because it defines only two of the five Mouse Listener methods. The Mouse Motion Handler implements the MouseMotionListener and defines both methods of that interface.

MouseTest.java

java.awt.event.MouseEvent

  • int getX()
  • int getY()
  • Point getPoint()

returns the x- (horizontal) and y- (vertical) coordinate, or point where the event happened, measured from the top -left corner of the component that is the event source.

  • int getClickCount()
    returns the number of consecutive mouse clicks associated with this event. (The time interval for what constitutes “consecutive” is system dependent.)

java.awt.event.InputEvent

  • int getModifiersEx()
    returns the extended or “down” modifiers for this event. Use the following mask values to test the returned value:
  • static String getModifiersExText(int modifiers)
    returns a string such as “Shift+Button1” describing the extended or “down” modifiers in the given flag set.

java.awt.Toolkit

hotSpot The cursor’s hot spot (such as the tip of an arrow or the center of cross hairs) name A description of the cursor, to support special accessibility environments

java.awt.Component

  • public Cursor create Custom Cursor (Image image, Point hotSpot, String name)
    creates a new custom cursor object.
  • public void setCursor(Cursor cursor)
    sets the cursor image to the specified cursor.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics