|
There are two interfaces that deal with mouse events: the "MouseListener" interface and the "MouseMotionListener" interface. Experiment with the applet below to see what it does. Then proceed to understand the code that generates the applet.
Using the event handling reference, we can see that the "MouseListener" interface requires five methods: "mousePressed", "mouseReleased", "mouseEntered", "mouseExited", and "mouseClicked". The "MouseMotionListener" interface requires two methods: "mouseDragged" and "mouseMoved". In the code below, the "init" method adds both a mouse listener and a mouse motion listener to the display. The next five methods are the methods that the "MouseListener" requires. In this particular example, only the "mousePressed" method is implemented in a nontrivial manner. It obtains the "x" and "y" coordinates of where the mouse was pressed and then uses these to decide whether to set the background color to red (the mouse was clicked in the top, left quarter), yellow (the mouse was clicked in the bottom, left quarter), blue (the mouse was clicked in the top, right quarter), or green (the mouse was clicked in the bottom, right quarter). Notice that there is some code that has been commented out in the "mouseClicked" method. This code examines how many times the mouse was clicked. If it was clicked once, the background color is set to white. Try running the program both with and without the statements that have been commented out to see what the effect of having two applicable methods is! The next two methods ("mouseDragged" and "mouseMoved") are the methods required by the "mouseMotionListener" interface. The "mouseMoved" method determines the current coordinates of the mouse and then calls "repaint" so that the current coordinates of the mouse will be displayed. Finally, the "paint" method displays the current values of the variables "x" and "y". These variables contain the current location of the mouse. import java.awt.*; import java.awt.event.*; import java.applet.*; public class Example extends Applet implements MouseListener, MouseMotionListener { public void init() { addMouseListener(this); addMouseMotionListener(this); } // mouseListener methods ------------------------------------ public void mousePressed (MouseEvent evt) { int x = evt.getX(); int y = evt.getY(); if ((x <= getSize().width / 2) && (y <= getSize().height / 2)) setBackground(Color.red); else if (x <= getSize().width / 2) setBackground(Color.yellow); else if (y <= getSize().height / 2) setBackground(Color.blue); else setBackground(Color.green); repaint(); } public void mouseReleased (MouseEvent evt) { } public void mouseEntered (MouseEvent evt) { } public void mouseExited (MouseEvent evt) { } public void mouseClicked (MouseEvent evt) { // if (evt.getClickCount() == 1) // setBackground(Color.white); // repaint(); } // mouseMotionListener methods --------------------- public void mouseMoved (MouseEvent evt) { x = evt.getX(); y = evt.getY(); repaint(); } public void mouseDragged (MouseEvent evt) { } // paint method ------------------------------------- public void paint (Graphics g) { g.drawString("x: ", 10, 50); g.drawString(Integer.toString(x), 25, 50); g.drawString("y: ", 10, 70); g.drawString(Integer.toString(y), 25, 70); } private int x; private int y; } |