int halfWidth = l.getWidth() / 2;
int halfHeight = l.getHeight() / 2;
int movementDistance = 10;// distance from the element in pixels
// check whether position left from the Element is valid
Locations moved = l.moveAllBy(-movementDistance, 0);
Point point = moved.getTopLeft();
if (point.x > 0) {
return new Point(-halfWidth - movementDistance, 0);
}
// check whether position right from the Element is valid
moved = l.moveAllBy(movementDistance, 0);
point = moved.getTopRight();
if (point.x < size.getWidth()) {
return new Point(halfWidth + movementDistance, 0);
}
// check whether position up from the Element is valid
moved = l.moveAllBy(0, -movementDistance);
point = moved.getTopRight();
if (point.y > 0) {
return new Point(0, -halfHeight - movementDistance);
}
// check whether position down from the Element is valid
moved = l.moveAllBy(0, movementDistance);
point = moved.getBottomRight();
if (point.y > size.getHeight()) {
return new Point(0, halfHeight + movementDistance);
}
throw new RuntimeException("Cannot find any suitable position for mouseout event.");
}