boolean (@NotNull MouseEvent me, @Nullable Rectangle rectangleOnScreen) { Point currentLocation = me.getLocationOnScreen(); // finding previous location Point previousLocation = null; for (int i = 0; i < HISTORY_SIZE; i++) { Point p = myHistory[(myCurrentIndex - i + HISTORY_SIZE) % HISTORY_SIZE]; if (p != null && p.distance(currentLocation) >= MOVEMENT_MARGIN_PX) { previousLocation = p; break; } } // storing current location myCurrentIndex = (myCurrentIndex + 1) % HISTORY_SIZE; myHistory[myCurrentIndex] = currentLocation; return ScreenUtil.isMovementTowards(previousLocation, currentLocation, rectangleOnScreen); }