1

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?

enter image description here

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");
        }
novicegeek
  • 773
  • 2
  • 9
  • 29
  • please post complete image, and let us know in that image what area is included in the panel you are listening through mouse listener – afzalex Sep 05 '15 at 12:48
  • I have updated the image now. The Heatmap panel is the complete think outlined in red and the actual HeatMap is with black border. I want the mouseListener to work only in the actual HeatMap and not in the whitespace which is visible outside the heatMap within the red border – novicegeek Sep 05 '15 at 12:52

1 Answers1

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.

Sanjay Manohar
  • 6,920
  • 3
  • 35
  • 58
  • I understood the section of the HeatMap source code where the dimensions of the actual heatMap are mentioned, but how do I convert this to get the coordinates? Should I send this 31,31drawImage parameters to setCoordinateBounds? Sorry, I am a little confused. – novicegeek Sep 05 '15 at 13:19
  • I used the following parameters in setCoordinateBounds intensityMap.setCoordinateBounds(31, intensityMap.getWidth(), 31,intensityMap.getWidth()); but still it didnot work. – novicegeek Sep 05 '15 at 13:50