Java HeatMap面板中HeatMap的MouseListener [英] MouseListener for HeatMap in Java HeatMap Panel

查看:188
本文介绍了Java HeatMap面板中HeatMap的MouseListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java类HeatMap(作者: http://www.mbeckler.org/heatMap/)为我的矩阵生成一个热图.我想实现一个鼠标侦听器,当鼠标位于图像(热图)上的某个位置时,该侦听器将显示坐标位置(x,y).目前,我已经实现了一个基本的鼠标侦听器,当鼠标指针在HeatMap面板中以及在HeatMap面板中时,它会显示一条消息.但是,问题是,HeatMap面板中的实际热图小于热图面板,并且还包含图例.我只想在将鼠标指针悬停在实际热图上而不是热图周围的区域时显示坐标信息.有人可以帮我吗?

I am using the Java class HeatMap (by: http://www.mbeckler.org/heatMap/) to generate a heatmap for my matrix. I want to implement a mouselistener which will display the coordinate position (x,y) when the mouse is at some position on the image (heatmap). I have implemented a basic mouse listener for the moment, which shows a message when the mouse pointer is in the HeatMap panel and when it is outside it. But, the problem is, the actual heatmap in the HeatMap panel is smaller than the heatmap panel and also includes a legend. I only want to display the coordinate information when the mouse pointer is hovered on the actual heatmap and not for the area surrounding the heatMap. Can someone help me do this?

下面是实现mouseListener和HeatMap面板的代码部分.

Below is the part of code which implements the mouseListener and the HeatMap panel.

public class GUI extends JFrame implements MouseListener {
    intensityMap = new HeatMap(dataMatrix, false,HeatMap.Gradient.GRADIENT_Rainbow);
                        intensityMap.setDrawLegend(true);
                        intensityMap.addMouseListener(this);
}

    public void mouseEntered(MouseEvent e) {
            System.out.println("Mouse entered");
        }

        public void mouseExited(MouseEvent e) {
            System.out.println("Mouse exited");
        }

推荐答案

因此,我查看了HeatMap的源代码.看来他已经完成了

So, I looked at the source code for HeatMap. It looks like he has done

  public void paintComponent(Graphics g){
    ...
    g2d.drawImage(bufferedImage,
                  31, 31,
                  width - 30,
                  height - 30,
                  0, 0,
                  bufferedImage.getWidth(), bufferedImage.getHeight(),
                  null);
    ...
    if (drawLegend) {
        g2d.drawRect(width - 20, 30, 10, height - 60);
        ...
    }

因此,您可以了解组件中组件的位置.

So this gives you an idea of where things will be within the component.

在鼠标侦听器中,您可以执行

in the mouse listener, you can do

public class GUI extends JFrame implements MouseListener, MouseMotionListener {
   public void mouseMoved(MouseEvent e){
      // e.getPoint().x, e.getPoint().y
   }
   public void mouseDragged(MouseEvent e){}
}

并在构造函数中执行

this.addMouseMotionListener(this);

获取坐标,然后可以使用这些数字(例如30/31等)并使用发送给setCoordinateBounds的值进行转换.

to get the coordinates, and you can then convert them using those numbers (30/31 etc) and using the values you sent to setCoordinateBounds.

这篇关于Java HeatMap面板中HeatMap的MouseListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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