如何在JMapViewer中向MapMarker添加工具提示 [英] How to add a ToolTip to MapMarker in JMapViewer

查看:132
本文介绍了如何在JMapViewer中向MapMarker添加工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 ToolTip 添加到 JMapViewer 上的自定义 MapMarker 。但重复搜索并没有帮我解决这个问题。

I'm trying to add a ToolTip to a custom MapMarker on JMapViewer. But repeaded searches on are not helping me solve this.

自定义 MapMarker 是:

public class MapMarkerUnit extends MapObjectImpl implements MapMarker

和Paint Method overide是

and the Paint Method overide is

public void paint(Graphics g, Point position, int radio) {
    String filename = "marker.png";
    //System.out.print(filename);
    BufferedImage x = null;
    try {
        x = ImageIO.read(getClass().getResource(filename));
    } catch (IOException ex) {
        Logger.getLogger(MapMarkerUnit.class.getName()).log(Level.SEVERE, null, ex);
    }

    g.drawImage(x, position.x-16, position.y-37,null);

    //if(getLayer()==null||getLayer().isVisibleTexts()) paintText(g, new       Point(position.x+20,position.y));
}

感谢您提供的任何帮助。

Thanks for any help your able to offer.

推荐答案

覆盖 JMapViewer的 getToolTipText()方法。在您的实现中,使用 getPosition() MouseEvent 坐标转换为大地坐标。以下示例仅显示未格式化的坐标;你需要找到最近的 MapMarker 并返回相应的文字。

Override the getToolTipText() method of JMapViewer. In your implementation, use getPosition() to convert the MouseEvent coordinates into geodetic coordinates. The example below simply displays the unformatted coordinates; you'll want find the nearest MapMarker and return the appropriate text.

JMapViewer map = new JMapViewer() {

    @Override
    public String getToolTipText(MouseEvent e) {
        Coordinate c = getPosition(e.getX(), e.getY());
        return c.getLat() + " " + c.getLon();
    }
};
map.setToolTipText(""); // initialize 

附录:有没有办法直接向图像添加工具提示?

否; JMapViewer 是处理工具提示的封闭 JComponent

No; JMapViewer is the enclosing JComponent that handles tool tips.

我在地图上有大约50个标记......这是很多迭代。

你绝对无法在<$ c中加载图像$ c> MapMarker 实施;使用 SWingWorker 在后台加载图片,用于示例

You definitely can't load images in your MapMarker implementation; use a SWingWorker to load images in the background, for example.

作为具体的迭代示例, JFreeChart 以这种方式轻松处理数十个实体的工具提示。这是封闭小组的 getToolTipText() 实现,这里是循环调用 Shape#contains()。可以在此处查看说明该方法的简单示例。

As a concrete iteration example, JFreeChart easily handles tool tips for scores of entities in this way. Here's the enclosing panel's getToolTipText() implementation, and here's the loop that invokes Shape#contains(). A simplified example that illustrates the approach is seen here.

这篇关于如何在JMapViewer中向MapMarker添加工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆