Mouse events in Java

Source: Internet
Author: User

A mouse event can occur on any component, for example, a mouse event occurs when you move the mouse into the component, exit the component, click the mouse over the component, or drag the mouse. That is to say, a widget can be an event source for mouse events.

1. Use the mouselistener interface to process mouse events

You can use the mouselistener interface to handle 5 mouse events:

(1) press the mouse button on the event source. Mousepressed (mouseevent)

(2) Open the mouse button on the event source. Mousereleased (mouseevent)

(3) click the mouse button on the event source. Mouseclicked (mouseevent)

(4) move the cursor to the event source. Mouseentered (mouseevent)

(5) Drop the event source with the mouse. Mouseexit (mouseevent)

The mouse event type is mouseevent, that is, when a mouse event occurs, the mouseevent class automatically creates an event object.

The mouseevent class has the following important methods:

(1) getx () obtains the X coordinate of the mouse in the event source coordinate system.

(2) Gety () obtains the Y coordinate of the mouse in the event source coordinate system.

(3) getmodifiers () obtains the left or right mouse button. The left and right mouse buttons are represented by the constants buttonl_mask and button3_mask in the inputevent class respectively.

(4) getclickcount () obtains the number of times the mouse is clicked.

(5) getsource () obtains the event source in which a mouse event occurs.

The event source obtains the monitor by addmouselistener (MONITOR ).

The mouselistener interface provides the following methods:

(1) mousepressed (mouseevent) is responsible for processing the mouse press event. That is, when you press the mouse at the event source, the monitor will automatically call this method in the interface to process the event after discovering the event.

(2) mousereleased (mouseevent) is responsible for handling mouse release events. That is, when you open the mouse in the event source, the monitor will automatically call this method in the interface to process the event after discovering the event.

(3) mouseentered (mouseevent) is responsible for processing the mouse-in event source events. That is, when the mouse moves in, the monitor will automatically call this method in the interface to process the event after discovering this event.

(4) mouseexited (mouseevent) is responsible for handling the mouse exit event. That is, when the mouse leaves the container, the monitor will automatically call this method in the interface to process the event after discovering this event.

(5) mouseclicked handles click events. That is, when the mouse is clicked, the monitor will automatically call this method in the interface to process the event after discovering this event.

Now we provide a mini program mouse1.java, which contains a text box that records mouse events. When the mouse enters the applet, the cursor area displays the Mouse A has come in. When the mouse leaves, the cursor area displays the mouse away. When the mouse is pressed, the cursor area displays "pressed mouse" and the coordinates of the mouse.

In the following example, in mouse2.java, when the left mouse button is pressed on the canvas, a circle is drawn at the mouse position. When the right mouse button is pressed, a rectangle is drawn at the mouse position: when you exit the canvas, clear all the drawn images.

In the following example, mouse3.java monitors mouse events on buttons, text boxes, and containers. When a mouse event occurs, it obtains the coordinates of the mouse. Note that the upper left corner of the coordinate system of the event source is the coordinate source point.

2. Use the mousemotionlistener interface to process mouse events

You can use the mousemotionlistener interface to handle mouse events in two ways:

(1) drag the mouse over the event source.

(2) move the mouse over the event source.

The mouse event type is mouseevent, that is, when a mouse event occurs, the mouseevent class automatically creates an event object. The event source obtains the monitor by addmousemotionlistener (MONITOR ).

The mousemotionlistener interface provides the following methods:

(1) mousedragged (mouseevent) handles the mouse drag event. That is, when you drag the mouse over the event source, the monitor will automatically call this method in the interface to process the event after discovering the event.

(2) mousemoved (mouseevent) handles mouse movement events. That is, when you move the mouse over the event source, the monitor will automatically call this method in the interface to process the event.

Here we also provide a very simple mini program mouse4.java that uses the mouse to paint. Our idea is very simple. As long as we can draw points with the mouse, we can draw freely with the mouse. We have used drawline (INT X1, int YL, int X2, int Y2) to draw a straight line from point (x1, Y1) to point (X2, Y2, when the starting point and the ending point of a straight line are the same, a point is drawn.

Now let's analyze the code in Example 4. When we drag the mouse (press the left mouse button), we get the coordinates when the mouse is dragged, and call the repaint () method to draw this coordinate point. We already know that when the repaint method is called in a applet, the program first clears the content previously drawn by the paint () method, and then calls the paint () method. But sometimes we don't want the program to clear all the content previously drawn by the paint () method. In fact, when we call

When the repaint () method is used, the program automatically calls the update (Graphics g) method (from the parent component) to clear the previously drawn content of the paint () method, and then calls the paint method. Therefore, we can rewrite this update (Graphics g) method in our small program (that is, the method that hides the parent class) and clear or save the parts as needed. In this example, we continue to call the paint method instead of clearing the previously drawn content in the updatte (Graphics g) method (if the clearrect method can be used for cleanup preparation, see section 17.4. Inspired by the above example, you may have thought that we can change the line width by drawing a solid circle. The following is a slightly complex example of mouse5.java. In this example, we can control the color of the line and use an eraser to erase the picture. The line width can be controlled with a slight improvement, and the shape of the mouse can also be controlled using the method described in the previous section.

3. Mouse event Transfer

If we are monitoring mouse events on a container, and some components are added to the container, When you click and move the mouse on the component, the container will not be aware of the occurrence of these operations.

You can use the mouse event to move the mouse event generated by an event source to another event source, that is, when you click the mouse on an event source, A mouse event can be triggered on another event source by moving the mouse event (hitting the west ). Use the static method of the swingutilities class in the javax. Swing package:

Mouseevent convertmouseevent (component source, mouseevent sourceevent, component destination)

You can move the mouse event on the source component to the component destination. This method returns the mouse event after the transfer. In the following example, in mouse6.java, you can click a button and drag the mouse to move the button to observe the mouse event transfer.

From: http://blog.ellechina.com/729720/2011/04/05/83222/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.