Implementing a mouse click event on a tile in a map

I'm trying to implement a mouse click event for an image (basically a tile on a map) on a JPanel. I'm just not able to figure out how to go about it. I have a Main class that extends JPanel. I'm retrieving tiles from a tile server and displaying them in the paintComponent() method of the Main class based on the specific zoom level. I use tiny locator images to represent a specific monument or a building in a city in the same paintComponent() method. They are placed on top of these tiles based on corresponding latitude and longitude.

When I click on these locator images, I must be able to add an event MouseClick() to the locator image. Now what I've read so far is we cannot add an event handler to images. It can only be added to native components of swing. How do I go about adding the events to the tiny locator images when I have not represented it using JLabel or even surrounding the image say with a rectangle?


On a sufficiently small map with a suitable projection, you can transform between coordinate systems using linear interpolation relative to the enclosing panel. Noting the following proportions, you can cross-multiply and solve for the missing coordinate, as shown in this complete example that maps mouse coordinates to pixel coordinates in an image.

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

More generally, use a library like JMapViewer that incorporates the map's projection into the transformation. A schema for hit-testing features is outlined here.