// assuming this code is inside a MouseListener method Point mouseLocation = e.getPoint(); // get the location of the mouse click Component deepestComponent = SwingUtilities.getDeepestComponentAt( e.getComponent(), mouseLocation.x, mouseLocation.y ); // deepestComponent variable will contain the component at mouse location // or null if no component was found
// assuming this code is inside a MouseMotionListener method Point mouseLocation = e.getPoint(); // get the location of the mouse Component componentUnderMouse = e.getComponent().getComponentAt(mouseLocation); // componentUnderMouse variable will contain the component at mouse location // or null if no component was found if (componentUnderMouse != null) { boolean isDeepest = componentUnderMouse.equals( SwingUtilities.getDeepestComponentAt( e.getComponent(), mouseLocation.x, mouseLocation.y ) ); // isDeepest variable will be true if the component is the deepest visible // component at mouse location, false otherwise }In the example above, we use the `getPoint()` method of the `MouseEvent` class to get the location of the mouse. Then, we use `getComponentAt()` method of the source component to get the component at that location. We compare this component with the deepest visible component using `equals()` method to determine whether it is the deepest or not. The `getDeepestComponentAt()` method is part of Java's standard library and is available in the `javax.swing` package.