0

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?

DNA
  • 42,007
  • 12
  • 107
  • 146
Marco Fedele
  • 2,090
  • 2
  • 25
  • 45
  • 4
    Why do you use `JComponent`s instead of `Graphics2D` primitives? It would be much better to handle, with a single `MouseListener` on your `JPanel`. – moonwave99 Sep 25 '12 at 23:03
  • The only way that I can think of doing it is to call `JComponent.processMouseEvent` manually on all the parent. This is messy, as you will need to translate the mouse points between the two components. Moonwave99's suggestion is far easier and generally, more suited to your needs – MadProgrammer Sep 25 '12 at 23:09
  • ...till you found this issue. – moonwave99 Sep 25 '12 at 23:24
  • I have yet implemented markers as extension of JComponent, and it works very well. The problem is with the lines. I must be able to move the objects when I come over them, and drag the mouse. With my experience with markers, I tought that it would be easy to use the same approach for the lines. So are you telling me it could be more easy to use Line2D in the Paint of JPanel, and keep trace of the lines? – Marco Fedele Sep 25 '12 at 23:27
  • 1
    Way better. You'll end up highlighting line's bounding box as well, and you'll have to move stuff on top of your render list each time, but it's easy logic once you got everything in same listener. – moonwave99 Sep 25 '12 at 23:35
  • 1
    As an example of @moonwave99's approach, see this [Q&A](http://stackoverflow.com/q/11439139/230513) and this [answer](http://stackoverflow.com/a/11944233/230513) – trashgod Sep 26 '12 at 00:24
  • 30 minutes and I have finished... thanks to all expecially to moonwave99!!! – Marco Fedele Sep 26 '12 at 00:39

1 Answers1

2

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.

Marco Fedele
  • 2,090
  • 2
  • 25
  • 45