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.
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()));
}
}
}