Search code examples
javaswingeventstransformationdelegation

Swing: Delegate events to child components of transformed parent


I've got a custom component GameViewCanvas extends JPanel that will hold a number of tiles (800x800 textures). Now in GameViewCanvas I've overridden the paint() method, so that it will paint the children on a transformed canvas (zoom, panning and rotation).

I now want to delegate events like MouseEvent and MouseMotionEvent to the respective children - the problem of course being that Swing has no idea where the children are on the actual frame. I thought I might be able to just override a method like getComponentAt in the parent but no such luck - I figure Swing is looping through the children directly?

Maybe I'm going about this all wrong, so please advice. Thanks in advance.


Solution

  • Take a look at JXLayer. It does exactly what you've just asked.

    There are some excellent examples by Piet Blok, which have, unfortunately, disappeared off the net. To this end, I've made them available here.

    Take a look at the TestWrapped demo in the pbjar source. It is probably the (among) the best example.

    enter image description here

    ps - JXLayer is now officially part of Java 7, known as JLayer