Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
437 views
in Technique[技术] by (71.8m points)

java - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...