2012-12-04 65 views
0

你好我想知道如何獲得鼠標在方法外的位置MouseClicked 這是我需要在有人點擊的地方繪製一個點..這是我現在使用的代碼。有人可以幫我弄這個嗎?在點擊畫一個點

Dot punt = new Dot(); 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 

    punt.setDot(g, getX(), getY(), 5); 

    repaint(); 
} 

@Override 
public void mouseClicked(MouseEvent m) { 
    // TODO Auto-generated method stub 
    gebeurtenisverslag.setText( 
      m.getClickCount() + " keer geklikt op " + m.getX() + "," + m.getY()); 
} 

回答

5

爲mouseClicked修改的類設置一個變量。

private int X; 
private int Y; 

@Override 
public void mouseClicked(MouseEvent m) { 
    gebeurtenisverslag.setText( 
      m.getClickCount() + " keer geklikt op " + m.getX() + "," + m.getY()); 
    X = m.getX(); 
    Y = m.getY(); 
} 

public void paintComponent(Graphics g) { 
    super.paintComponent(g); 
    //Probably want to add logic here to verify X & Y have been set 
    punt.setDot(g, X, Y, 5); 
    repaint(); 
} 
+0

打我吧+1 – fireshadow52