Sign in to follow this  

[java] JComponents on Zoomable Map

This topic is 3726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone, Right now I'm working on making a zoomable map for use in strategy games. I am able to zoom in and out successfully. However, I am having trouble using mouse events with JComponents built into the map. I would like to use the built-in behavior for mouse-entered and mouse-exited events to highlight and de-highlight the JComponents. My problem is that there seems to be no obvious way to pre-translate the mouse coordinates to compensate for the current zoom factor. Regardless of the zoom factor, mouse-entered and mouse-exited events are dispatched when the mouse pointer is in the original bounds of the JComponent. Does anyone know the origin of mouse-entered and mouse-exited events in Swing? Are they created within Java or do they come from the native OS? Also, is there any easy way to "forward" mouse events from one component to another? Would it be easier to roll my own classes for selectable map components? Thanks in advance for your responses. :) - Rob

Share this post


Link to post
Share on other sites
I'm pretty sure that any and all input events would have to originate from the OS. However, I believe in Swing that it's Java and not the OS who decides if a JComponent is the target of an event. Swing is lightweight, so there aren't any native peers to tell Java anything.
Some of the components have protected methods on them like processKeyEvent() and processMouseMotionEvent(). You can always subclass the JComponent and override these methods as public. However, make sure that the default behavior is called as well (through super.processMouseMotionEvent()).
I haven't tried anything like that above, but I'm sure it's been done before with custom drawn components.

Share this post


Link to post
Share on other sites
Don't you "zoom" the map JComponents too though?

I've just finished doing exactly the same as you, but I increase or decrease the size of any present JComponents on the map (setBounds), so as I zoom in, my (e.g.) JLabels increase in size, zoom out, they decrease.

I've no problems with mouse entering/exiting detection...

Or perhaps I'm misunderstanding you...

Share this post


Link to post
Share on other sites

This topic is 3726 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this