Exercise 4: Mouse Input

Home/Java - Tutorial/Java - Applet/Exercise 4: Mouse Input
Exercise 4: Mouse Input 2015-12-04T17:41:00+00:00

[ Home | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 ]

The source code:

(Download the below file: Mouse1.java)

Try clicking and dragging on the resulting applet. Notice how the status bar in your web browser displays the current mouse position — that’s due to the calls to showStatus(). (You might see some occasional flickering in this applet. This problem will be addressed in an upcoming lesson.)


The MouseEvent data that gets passed into each of the mouse*() functions contains information on the position of the mouse, the state of the mouse buttons and modifier keys (i.e. the Shift, Alt, Ctrl, and Meta keys), the time at which the event occurred, etc. To find out how to access this information, go here.

Another example:

(Download the below file: Mouse2.java)

Try clicking and dragging on the gray square:


A third example:

(Download the below file: Mouse3.java)

Move freely over the applet. Notice that moving faster makes the line stretch out longer.