为什么当我运行这个小型鼠标挂钩应用程序时,我的鼠标会滞后? [英] Why is my mouse lagging when I run this small mouse hook application?

查看:24
本文介绍了为什么当我运行这个小型鼠标挂钩应用程序时,我的鼠标会滞后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我几年前写的一个小鼠标挂钩应用程序,我只是想知道为什么每次运行它都会让我的鼠标滞后.

This is a little mouse hook application that I wrote a few years ago and I just was wondering why it makes my mouse lag whenever I run it.

我记得在某处读到过,我必须调用一些方法来手动处理资源或使用 MouseListener 进行处理.每当我在屏幕上拖动任何窗口时,它都会使我的鼠标滞后,而当它不运行时不会发生这种情况.知道为什么吗?(我知道我在 EDT 上运行了一个 while 循环,我的 2 个 JLabel 的变量名称是 J 和 C,起诉我)

I remember reading somewhere that I have to call some method to manually dispose of resources or something with the MouseListener. It will make my mouse lag whenever I drag any window around the screen, and this doesn't happen when it's not running. Any idea why? (I know I'm running a while loop on the EDT and my variable names for the 2 JLabels are J and C, sue me)

import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class MouseLocation {

    Point p;
    int x,y;

    MouseLocation() throws AWTException {

    }

    public String printLocation(){
        p = MouseInfo.getPointerInfo().getLocation();
        x = p.x;
        y = p.y;
        String location = (x + " - " + y);

        return location;
    }

    public Color getMouseColor() throws AWTException{
        Robot r = new Robot();
        return r.getPixelColor(x, y);
    }



    public static void main(String[] args) throws AWTException {
        MouseLocation m = new MouseLocation();

        JFrame frame = new JFrame("Mouse Location Display");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(450,110);
        frame.setLayout(new FlowLayout());
        JLabel j = new JLabel();
        JLabel c = new JLabel();

        j.setFont (j.getFont ().deriveFont (24.0f));
        c.setForeground(Color.red);

        frame.add(j);
        frame.add(c);
        frame.setVisible(true);
        while (true){
            j.setText("Current Mouse Location: " + m.printLocation());

            c.setText(String.valueOf(m.getMouseColor()));
        }
    }
}

推荐答案

您以非常快的速度请求鼠标位置.尝试在循环中添加 Thread.sleep(time) :

You are requesting the mouse position at a very fast rate. Try adding a Thread.sleep(time) in your loop:

while (true){
    j.setText("Current Mouse Location: " + m.printLocation());
    c.setText(String.valueOf(m.getMouseColor()));

    // waiting a few milliseconds
    Thread.sleep(200);
}

此外,最佳做法是重用对象以避免重新分配.你可以像这样改进你的方法 getMouseColor:

Also, it's best practice to reuse objects to avoid reallocation. You could improve your method getMouseColor like this:

// Global var
Robot robot;

MouseLocation() throws AWTException {
    robot = new Robot();
}

public Color getMouseColor() {
    return robot.getPixelColor(x, y);
}

按照@cricket_007 的建议,使用计时器来避免在主线程(以及 while 循环内)中使用 Thread.sleep:

Following the suggestion by @cricket_007, use a timer to avoid using a Thread.sleep in the main thread (and inside a while-loop):

new Timer().schedule(new TimerTask() {

    @Override
    public void run() {
        j.setText("Current Mouse Location: " + m.printLocation());
        c.setText(String.valueOf(m.getMouseColor()));
    }
}, 0, 200); // 200 milliseconds

这篇关于为什么当我运行这个小型鼠标挂钩应用程序时,我的鼠标会滞后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆