JDK-4059534 : mouseEntered and mouseExited events contain incorrect coordinates
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.1.1,1.1.5,1.1.6
  • Priority: P4
  • Status: Closed
  • Resolution: Cannot Reproduce
  • OS: generic,windows_95,windows_nt
  • CPU: generic,x86
  • Submitted: 1997-06-17
  • Updated: 2000-06-12
  • Resolved: 2000-06-12
Related Reports
Duplicate :  
Relates :  
Description

Name: sgC58550			Date: 06/17/97


1) The coordinates in a mouseEntered event when the mouse enters
from a different AWT component are in the coordinate space
of the component being exited, not the component being entered.

2) The coordinates in a mouseExited event when the mouse is
being moved from the AWT component to a native Windows95
application window are in the coordinate space of the native
Windows95 application window.

3) Also, when dragging, there is an extraneous mousePressed
event sent to a component immediately after the mouseExited
event.

These observations are based on examining the output of a
program with a Canvas object containing the following code.


    ...

    addMouseListener(this);
    addMouseMotionListener(this);

    ...

    public void mouseEntered(MouseEvent evt)
    {
        int x = evt.getX();
        int y = evt.getY();
        System.out.println("mouseEntered: " + x + ", " + y);
    }

    public void mouseExited(MouseEvent evt)
    {
        int x = evt.getX();
        int y = evt.getY();
        System.out.println("mouseExited: " + x + ", " + y);
    }

    public void mousePressed(MouseEvent evt)
    {
        int x = evt.getX();
        int y = evt.getY();
        System.out.println("mousePressed: " + x + ", " + y);
    }

company - Steam Communications Inc. , email - ###@###.###
======================================================================

sheri.good@Eng 1997-06-26

Release: 1.1.1
Hardware: PC
OSversion: NT

Synopsis: mouseEntered() gets wrong coordinates

Description: 
The mouseEntered() Method of a MouseListener receives
incorrect coordinates when registered on a Component. It seems as if they
are relative to a parent Component. The Coordinates of
MouseMotionListener-Events are correct.



Name: krT82822			Date: 10/09/99


To Reproduce Problem:

Extend java.awt.Window.
Catch mouse entered events.
Obtain (x, y) via getX(), getY() of MouseEvent.

Actual Problem:

The obtained (x, y) coordinate is not relative to
the window, or even to the screen.
The values are given by:
       x = winLocation.x * 2 + relativeX
       y = winLocation.y * 2 + relativeY
The values should be:
       x = relativeX
       y = relativeY

Example Application:
/***************************
 ******** Copy This ********
 ***************************/

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

public class WindowErrorTest
{
	public static void main(String[] args)
	{
		Frame f = new Frame("WindowErrorTest");
		TextArea text = new TextArea();

		text.append("Move into and out of the window (on right).\n\n");
		text.append("Window size: 200x200, Window Pos: (400, 200)\n\n");
		text.append("event.getX() returns:\n");
		text.append("           x = winPos.x * 2 + relativeX\n\n");
		text.append("event.getY() returns:\n");
		text.append("           y = winPos.y * 2 + relativeY\n\n");

		f.add("Center", text);

		Window_Subclass w = new Window_Subclass(f, text);
		w.setBackground(Color.gray);

		f.addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent ev)
				{
					System.exit(0);
				}
			});

		f.setSize(350, 400);
		f.setLocation(0, 0);
		f.show();
		w.setSize(200, 200);
		w.setLocation(400, 200);
		w.show();
	}
}

class Window_Subclass extends Window
{
	TextArea text;

	public Window_Subclass(Frame f, TextArea text)
	{
		super(f);

		this.text = text;

		enableEvents(AWTEvent.MOUSE_EVENT_MASK);
	}

	public void processMouseEvent(MouseEvent ev)
	{
		if (ev.getID() == MouseEvent.MOUSE_ENTERED)
			text.append("MOUSE_ENTERED: ev.getX(): " + ev.getX() + ", ev.getY(): " + ev.getY() + "\n");
			// + ", relative X: " + (ev.getX() - 200) + ", relative Y: " + (ev.getY() - 200)
		super.processMouseEvent(ev);
	}

	public void paint(Graphics g)
	{
		g.drawOval(-3, -3, 7, 7);
		FontMetrics fm = getToolkit().getFontMetrics(getFont());
		g.drawString("(0, 0)", 5, fm.getMaxAscent());
		super.paint(g);
	}
}

/*******************************
 ********** End Code ***********
 *******************************/
(Review ID: 43213)
======================================================================

Comments
EVALUATION 6/12/2000 kevin.ryan@eng -- points #1 and #3 are covered in bug #4083025, and are no longer reproducible as of 1.1.8_003. Point #2 is no longer reproducible as of 1.1.8_003.
11-06-2004

WORK AROUND Name: sgC58550 Date: 06/17/97 Basically, this means programs can't depend on the coordinates contained in a mouseEnter or mouseExit event. Which is a serious problem if you have an application that wants to know which edge of it boundary the mouse has been moved over. ====================================================================== Name: krT82822 Date: 10/09/99 I have used the mouse moved event to obtain the correct position of entry. (Review ID: 43213) ======================================================================
11-06-2004

SUGGESTED FIX he bug occurs in AwtToolkit::PreProcessMouseMsg() because the value in mouseLParam is converted to the coordinate space of the component just exited, for synthesising the EXIT event, but then is used in this inappropriate "mode" when synthesising the ENTER event. In the drag case it is recalculated, but in the move case the logic says that since the component on whichthe message was delivered and the mouse component (just entered) are the same, the coordinates don't need transforming. Well .. this was true when you entered the method, but we just overwrote that value! in src/win32/sun/windows/awt_Toolkit.cpp we have if (mouseComp) { if (p && mouseComp && p != mouseComp) { // hit bug if true // .. do the transform and it'll be OK } mouseComp->SendMessage(WM_AWT_MOUSEENTER, mouseWParam, mouseLParam) Suggested fix: right after sending the exit message reset the value in mouseLParam by adding the line:- mouseLParam = msg.lParam;
11-06-2004

PUBLIC COMMENTS not reproducible as of 1.1.8_003 (see also #4083025)
10-06-2004