I created a MouseMotionDetection class which role is just to detect that the user has moved the mouse anywhere on screen.
For this purpose I created a new JFrame inside my class constructor with the screen size which is invisible, so basically I am observing mouse motion all over the screen.
But, I have a weird bug:
In the code's current form, once this class is activated I only detect ONE mouse motion and nothing else, it stops working right after that. But, if I put the line which sets the frame backfround to 0f,0f,0f,0f (transparent) in comments and then activate, the whole screen becomes grey and I keep tracking all the mouse motions just as I desired (I just can't see anything).
I really do not understand why this happens, haven't seen related issues around, nor in this related javadoc, which discusses MouseMotion events.
This is the code:
public class MouseMotionDetection extends JPanel
implements MouseMotionListener{
public MouseMotionDetection(Region tableRegion, Observer observer){
addMouseMotionListener(this);
setBackground(new Color(0f,0f,0f,0f));
JFrame frame = new JFrame();
frame.setUndecorated(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize);
frame.setBackground(new Color(0f,0f,0f,0f));
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setAlwaysOnTop(true);
JComponent contentPane = this;
contentPane.setOpaque(true);
frame.getContentPane().add(contentPane, BorderLayout.CENTER);
frame.setVisible(true);
}
@Override
public void mouseDragged(MouseEvent arg0) {
}
@Override
public void mouseMoved(MouseEvent arg0) {
System.out.println("mouse movement detected");
}