6

我创建了一个 MouseMotionDetection 类,它的作用只是检测用户是否将鼠标移动到屏幕上的任何位置。

为此,我在我的类构造函数中创建了一个新的 JFrame,其屏幕大小是不可见的,所以基本上我在整个屏幕上观察鼠标运动。

但是,我有一个奇怪的错误:

在代码的当前形式中,一旦激活了这个类,我只检测到一个鼠标移动而没有其他任何东西,它在那之后就停止工作。但是,如果我将框架背景设置为 0f,0f,0f,0f(透明)的行放在评论中然后激活,整个屏幕就会变灰,我会按照我的意愿继续跟踪所有鼠标动作(我只是可以什么都看不见)。

我真的不明白为什么会发生这种情况,没有看到相关的问题,也没有在这个讨论 MouseMotion 事件的相关 javadoc 中看到。

这是代码:

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");
}
4

2 回答 2

11

完全透明的框架不接收鼠标事件。

这是使用MouseInfo. 这适用于应用程序的组件。是不可见的(透明的)、不集中的或最小化的。

在此处输入图像描述

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.GeneralPath;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class MouseMoveOnScreen {

    Robot robot;
    JLabel label;
    GeneralPath gp;
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    MouseMoveOnScreen() throws AWTException {
        robot = new Robot();

        label = new JLabel();
        gp = new GeneralPath();
        Point p = MouseInfo.getPointerInfo().getLocation();
        gp.moveTo(p.x, p.y);
        drawLatestMouseMovement();
        ActionListener al = new ActionListener() {

            Point lastPoint;

            @Override
            public void actionPerformed(ActionEvent e) {
                Point p = MouseInfo.getPointerInfo().getLocation();
                if (!p.equals(lastPoint)) {
                    gp.lineTo(p.x, p.y);
                    drawLatestMouseMovement();
                }
                lastPoint = p;
            }
        };
        Timer timer = new Timer(40, al);
        timer.start();
    }

    public void drawLatestMouseMovement() {
        BufferedImage biOrig = robot.createScreenCapture(
                new Rectangle(0, 0, d.width, d.height));
        BufferedImage small = new BufferedImage(
                biOrig.getWidth() / 4,
                biOrig.getHeight() / 4,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = small.createGraphics();
        g.scale(.25, .25);
        g.drawImage(biOrig, 0, 0, label);

        g.setStroke(new BasicStroke(8));
        g.setColor(Color.RED);
        g.draw(gp);
        g.dispose();

        label.setIcon(new ImageIcon(small));
    }

    public JComponent getUI() {
        return label;
    }

    public static void main(String[] args) throws Exception {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                JPanel ui = new JPanel(new BorderLayout(2, 2));
                ui.setBorder(new EmptyBorder(4, 4, 4, 4));

                try {
                    MouseMoveOnScreen mmos = new MouseMoveOnScreen();
                    ui.add(mmos.getUI());
                } catch (AWTException ex) {
                    ex.printStackTrace();
                }

                JFrame f = new JFrame("Track Mouse On Screen");
                // quick hack to end the frame and timer
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.setContentPane(ui);
                f.pack();
                f.setLocationByPlatform(true);
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
于 2014-08-24T03:24:56.743 回答
3

我相信不会为透明像素生成 MouseEvents。也就是将 MouseEvent 调度到框架下的“可见”组件。

因此,要接收事件,您不能使用绝对透明度。但是您也许可以使用 1 的 alpha 值。我怀疑您会注意到“透明框架”的绘制有所不同。

所以我会使用如下代码:

//frame.setBackground(new Color(0f,0f,0f,0f));
frame.setBackground(new Color(0f, 0f, 0f, 1f));

不需要以下内容,因为您在将“contentPane”添加到框架时将其设置为透明:

//setBackground(new Color(0f,0f,0f,0f));

应该注意的是,此代码仅在您的应用程序具有焦点时才有效。

于 2014-08-24T02:50:19.687 回答