我正在使用Java类HeatMap (by:http://www.mbeckler.org/heatMap/)为我的矩阵生成一个热图。我想要实现一个mouselistener,它将显示坐标位置(x,y),当鼠标在图像上的某个位置时(热图)。目前,我已经实现了一个基本的鼠标侦听器,它显示了鼠标指针在HeatMap面板中和在面板之外时的消息。但是,问题是,heatmap面板中的实际热图比热图面板要小,并且还包括一个图例。我只想在鼠标指针在实际热图上盘旋时显示坐标信息,而不想显示heatMap周围的区域。有人能帮我吗?

下面是实现mouseListener和HeatMap面板的代码的一部分。
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");
}发布于 2015-09-05 13:02:32
因此,我查看了HeatMap的源代码。看上去他做了
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);
...
}因此,这可以让您了解组件中的东西在哪里。
在鼠标侦听器中,您可以
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的值来转换它们。
https://stackoverflow.com/questions/32413199
复制相似问题