问题
I have a JPanel with null layout. On this panel I draw some custom JComponents as markers, little red rounds, and lines.
When the line is not horizontal or vertical, the JComponent's "sensitive area" is a rectangle which has as hypotenuse the line itself. This excessive area often "steals" MouseEvents to the markers.
How can I make MouseEvents "pass" to the covered objects, if the user is not clicking the real line?
回答1:
The correct approach is that suggested by moonwave99.
1) Create and mantain an array of lines.
2) Draw every line in the array overriding the paintComponent method of JPanel using Graphics2D primitives.
3) Add a MouseListener to the JPanel, with a research function in the line's array, to know which line is selected.
来源:https://stackoverflow.com/questions/12592307/java-mouse-hit-detection-with-overlapping-jcomponents