停止滑鼠 [英] Stopping mouseMoved
问题描述
我想知道如何阻止mousedMoved被解雇.我一直在谷歌搜索,但找不到答案.有办法吗?我正在使用eclipse,并通过了mouseevent方法,但什么都找不到.
I was wondering how to stop mousedMoved from being fired. I've been googling but can't find an answer. Is there a method for this? I'm using eclipse and went through the mouseevent methods but just can't find anything.
public class Drawing extends JPanel {
private ArrayList<Point> pointList;
private int counter = 0;
public Drawing() {
setLayout(new FlowLayout());
setBackground(Color.white);
pointList = new ArrayList<Point>();
addMouseListener(new MouseTrackerListener());
}
public void paintComponent(Graphics pen) {
super.paintComponent(pen);
for (int i = 0; i < pointList.size(); i++) {
Point p = pointList.get(i);
pen.fillOval(p.x, p.y, 10, 10);
}
}
private class MouseTrackerListener extends MouseInputAdapter {
public void mouseClicked(MouseEvent e) {
counter++;
if (counter % 2 != 0) {
addMouseMotionListener(new MouseTrackerListener());
} else {
System.out.println("Hi");
}
}
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
pointList.add(point);
repaint();
}
}
推荐答案
您可以创建一个boolean
来切换是否处于图形状态.您将boolean
命名为isDrawingMode
you can create a boolean
to toggle if it's on drawing status or not. you name the boolean
like isDrawingMode
因此,当您单击鼠标时..将其设置为false,如果再次单击它,它将变为true;
so when you click the mouse.. you set it to false, if you click it again, it will become true;
您要做的就是单击鼠标时切换boolean isDrawingMode
all you have to do is to toggle the boolean isDrawingMode
when the mouse was clicked
所以您的mousemoved listener
看起来像这样
public void mouseMoved(MouseEvent e) {
if (!isDrawingMode) return; //if isDrawingMode is false, it will not trigger to draw
Point point = e.getPoint();
pointList.add(point);
repaint();
}
这篇关于停止滑鼠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!