Introducing a mouse click event on a tile on a map

I am trying to implement a mouse click event for an image (mainly a tile on a map) on a JPanel. I just can't figure out how to do this. I have a Main class that extends JPanel. I retrieve the tiles from the tile server and display them in the paintComponent () method of the Main class based on a specific zoom level. I use tiny locator images to represent a specific monument or building in a city in the same paintComponent () method. They are placed on top of these tiles based on the appropriate latitude and longitude.

When I click on these locator images, I should be able to add the MouseClick () event to the locator image. Now that I have read so far, we cannot add an event handler to the images. It can only be added to your own swing components. How do I add events to tiny locator images when I did not represent it using JLabel or even around an image with a rectangle?

+1
java swing paintcomponent mouseevent
source share
1 answer

On a fairly small map with a suitable projection, you can convert between coordinate systems using linear interpolation relative to the closing panel. Given the following proportions, you can cross-multiply and solve for the missing coordinate, as shown in this full example , which displays the mouse coordinates in pixel coordinates in the image.

mouse.x : panelWidthInPixels :: featureLongitude : tileWidthInDegrees mouse.y : panelHeightInPixels :: featureLatitude : tileHeightInDegrees 

More generally, use a library such as JMapViewer , which includes map projection into a transform. The circuitry for testing functions during testing is presented here .

+3
source share

All Articles