2015-09-05 72 views
1

我使用Java類HeatMap(:http://www.mbeckler.org/heatMap/)爲我的矩陣生成熱圖。我想實現一個mouselistener,當鼠標位於圖像的某個位置(熱圖)上時,它將顯示座標位置(x,y)。我現在已經實現了一個基本的鼠標監聽器,當鼠標指針位於HeatMap面板中並且它位於其外部時,它會顯示一條消息。但問題是,HeatMap面板中的實際熱圖小於熱圖面板,並且還包含一個圖例。我只想在鼠標指針懸停在實際熱圖上時顯示座標信息,而不顯示heatMap周圍的區域。有人可以幫我做這個嗎?Java中的HeatMap的MouseListener HeatMap面板

enter image description here

下面是代碼的一部分,其實現的MouseListener和熱圖面板。

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

請發佈完整圖像,並通過該圖像告訴我們您正在通過鼠標聽衆收聽的面板中包含的區域 – afzalex

+0

我已經更新了圖像。 Heatmap面板是以紅色概括的完整想法,而實際的HeatMap帶有黑色邊框。我希望mouseListener僅在實際的HeatMap中工作,而不是在紅色邊框內的heatMap外部可見的空白處工作 – novicegeek

回答

1

所以,我看了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的值。

+0

我瞭解提及實際heatMap尺寸的HeatMap源代碼部分,但我如何將其轉換爲座標?我應該將此31,31drawImage參數發送給setCoordinateBounds嗎?對不起,我有點困惑。 – novicegeek

+0

我在setCoordinateBounds 中使用了以下參數:intensityMap.setCoordinateBounds(31,intensityMap.getWidth(),31,intensityMap.getWidth()); 但仍然沒有工作。 – novicegeek

+0

它現在工作! – novicegeek