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)
======================================================================