How can I find the position of the mouse without a MouseEvent?

Rob Edmondson

The JVM provides all information regarding the system's mouse within an applet (or application) via mouse events. There is no way to pole the JVM for mouse information directly. An easy way around this is to implement the MouseListener and MouseMotionListener interfaces to maintain mouse state information that can be queried at any time.

For example:


public class MouseDemo extends Applet implements MouseListener, 
MouseMotionListener
{
    .   //Your Applet
    .
    .
    MouseEvent CurrentMouseEvent=null;
    Boolean IsMouseValid = false;

    public void mouseEntered(MouseEvent e)
    {
        IsMouseValid = true;
        CurrentMouseEvent = e;
    }

    public void mouseExited(MouseEvent e)
    {
        IsMouseValid = false;
        CurrentMouseEvent = e;
    }

    public void mousePressed(MouseEvent e)
    {
        IsMouseValid = true;
        CurrentMouseEvent = e;
    }

    public void mouseReleased(MouseEvent e)
    {
        IsMouseValid = true;
        CurrentMouseEvent = e;
    }

    public void mouseDragged(MouseEvent e)
    {
        IsMouseValid = true;
        CurrentMouseEvent = e;
    }

    public void mouseMoved(MouseEvent e)
    {
        IsMouseValid = true;
        CurrentMouseEvent = e;
    }

    public Point getMousePosition(boolean bValidate)
    {
        if(bValidate && !IsMouseValid)
            return null;
        else
            if(CurrentMouseEvent != null)
                return CurrentMouseEvent.getPoint();
    }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact