MouseListener for HeatMap in Java HeatMap Panel

白昼怎懂夜的黑 提交于 2019-12-11 13:45:32

问题


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?

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");
        }

回答1:


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){}
}

and in the constructor do

this.addMouseMotionListener(this);

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



来源:https://stackoverflow.com/questions/32413199/mouselistener-for-heatmap-in-java-heatmap-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!