Java: mouse hit detection with overlapping JComponents

孤者浪人 提交于 2019-12-25 16:58:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!