I can't answer how exactly Java detects click, but your Questions sounds like you want a nice way of handling clicks in a custom painted JPanel.
I have the following suggestion:
Whenever you draw clickable objects, you draw the same shape in a specific color in a "feedback buffer" texture. The color represents the button/clickable id. This is used in 3D applications as well.
The drawing order then equals the z-order of your UI elements.
After drawing, you may click the JPanel, and use the feedback buffer to evaluate the clicked element:
Get pixel color at clicked position, look into your color-id array, and tell the element with the id that a click occurred.
This is pseudo example image of a button layout and a feedback buffer:
Note how even stacked buttons and windows overwrite previously drawn pixels. Using this technique ensures your click-evaluation respects the button-z order and even window frames.
Also note that the performance while evaluating a click will be independent from the amount of overlapping buttons.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…