20-CS-694 Advanced Programming Techniques Spring 2012
Mouse Manipulation

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

Previous     Next     All lectures

Listening to and Acting on Mouse Events

    1.   ShowMouseCoords.java

    Acting on mouse movement - display mouse coordinates. A JPanel is placed in the center of a JFrame's BorderLayout. The JPanel listens for mouse movements and reports current mouse positions to the JTextFields in a JPanel that is placed in the north section of the JFrame's BorderLayout.

Instructions: Move the mouse cursor. The changing position shows up in the two left JTextFields.

 
    2.   ShowMousePressed.java

    The MouseListener interface can supply mouse information in the event of mouse clicks. But the MouseListener interface requires implementing five methods, only one of which is needed in this example. So, an inner MouseAdapter class is created here with just the mousePressed method overridden and addMouseListener takes a reference to an object of that class as argument (instead of this). Every click of the mouse in the MousePad JPanel adds 1 to a count that is displayed in a JTextField in the north section of the JFrame.

Instructions: Move the mouse cursor and periodically click the mouse button. The upper right text field records the count. Edit the count text field, replacing the number showing with a string such as "Hello World." The next time the mouse is clicked a NumberFormatException is raised.

 
    3.   ShowMousePressed_1.java

    Our own mouse adapter - saves on implementing lots of functions we do not need but is still awkwardly placed outside of the class in which it is used so it needs a reference to a MousePad_1 object.

Instructions: Move the mouse cursor and periodically click the mouse button. The upper right text field records the count. Edit the count text field, replacing the number showing with a string such as "Hello World." The next time the mouse is clicked a NumberFormatException is raised.

 
    4.   ShowMousePressed_2.java

   

Instructions: Move the mouse cursor and periodically click the mouse button. The upper right text field records the count. Edit the count text field, replacing the number showing with a string such as "Hello World." The next time the mouse is clicked a NumberFormatException is raised.

 
    5.   ShowMousePressed_3.java

    Our own mouse adapter - implements the MouseListener interface so as not to clutter up MousePad_3 with useless functions. Still, there is clutter.

Instructions: Move the mouse cursor and periodically click the mouse button. The upper right text field records the count. Edit the count text field, replacing the number showing with a string such as "Hello World." The next time the mouse is clicked a NumberFormatException is raised.

 
    6.   ShowMousePressed_4.java

    Require services of MouseListener and MouseMotionListener

Instructions: Move the mouse cursor and periodically click the mouse button. The upper right text field records the count. Edit the count text field, replacing the number showing with a string such as "Hello World." The next time the mouse is clicked a NumberFormatException is raised.

 
    7.   InputException.java

    Acting on a mouse click - adding an exception to catch input error. The problem with the previous applet is that 1 cannot be added to a String object and will cause an exception if attempted. In the code below that exception is caught and handled gracefully (by setting the click count to 0).

Instructions: Same as previous applet. This time the exception is caught and remedial action (set the count text field to 0) is taken.

 
    8.   MouseClick.java

    Acting on a mouse click - adding a button. A JButton is added and an ActionListener is implemented (method actionPerformed) to handle button pressed. This example puts the code of the MousePad class inside main class (namely MouseClickFrame).

Instructions: Click the reset button to set the mouse click count to 0.