Why doesn't it display the output for mouseReleased and mouseClicked in console? Is there something that I am missing?

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class MouseMotion extends Applet implements MouseMotionListener{

    public void init() {
        addMouseMotionListener(this);
        }

    public void mouseDragged(MouseEvent e) {
        System.out.println("mouse is being dragged at location (" + e.getX() + ", " + e.getY() + ")");
        }

    public void mouseMoved(MouseEvent e) {
        System.out.println("mouse is being moved at location (" + e.getX() + ", " + e.getY() + ")");
        }

    public void mouseReleased(MouseEvent e){
        System.out.println("Mouse has been released at location (" + e.getX() + ", " + e.getY() + ")");
        }

    public void mouseClicked(MouseEvent e) {
        System.out.println("Mouse has been clicked at location (" + e.getX() + ", " + e.getY() + ")");
        }

}

Recommended Answers

All 4 Replies

MouseListener This interface defines five methods:

public void mouseEntered(MouseEvent event)
public void mouseExited(MouseEvent event)
public void mousePressed(MouseEvent event)
public void mouseReleased(MouseEvent event)
public void mouseClicked(MouseEvent event)

The mouseEntered and mouseExited methods correspond to the event of the mouse entering or exiting the window or component that is using the listener. Note that mouseExited will be called when the mouse enters a component that is on top of the current one, not just when mouse leaves the outside boundaries of the window. For example, suppose you put a button in an applet and move the mouse from outside the applet onto the button and then back outside the applet. The applet will first receive a mouseEntered event (when the mouse pointer enters the boundaries of the applet), then a mouseExited event (when the mouse moves over top of the button), then a second mouseEntered event (when the mouse leaves the button and is back over the main part of the applet), and then a second mouseExited event (when the mouse leaves the boundaries of the applet). Note that the x and y locations reported by mouseExited can be outside the applet boundaries; note, too, that Netscape often gives nonsensical values for the x and y positions of mouseExited.

The mousePressed method corresponds to the event of the mouse button first being pressed, mouseReleased corresponds to the mouse button release, and mouseClicked corresponds to mouse button release after it was pressed in the current location. So, for example, if you press and release the mouse without moving it, mousePressed, mouseReleased, and mouseClicked all get triggered (in that order). If, however, you press the mouse, drag it, and then release it, mousePressed and mouseReleased still get triggered, but mouseClicked doesn't.

From "Core Web Programming - Google Books Result" Marty Hall, Larry Brown

what is this i don't know .

In above Program the applet does not implements MouseListener Interface

MouseListener and MouseMotionListener both interface are different.

MouseListener Interface has following method

public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}

MouseMotionListener Interface has following method

public void mouseMoved(MouseEvent e) {}
public void mouseDragged(MouseEvent e) {}

So the program implement MouseMotionListener but they use MouseListener's method.

Does the applet have the focus so that is can get mouse events?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.