Reputation: 2652
Hello I want to know how to get the position of the mouse outside the method MouseClicked this I need for drawing a dot at the place where someone clicks.. this is the code that I use now. Can someone help me with this?
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());
}
Upvotes: 0
Views: 431
Reputation: 4524
Set a variable for the class that mouseClicked modifies.
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();
}
Upvotes: 5