java实现获取鼠标在屏幕上的坐标

本篇博客其实没什么难度可言,在这里分享给大家,是因为有时候我们需要这个工具,java作为跨平台语言的优势在这个软件就可以体现出来,不需修改就可以在windows、mac、linux上使用这个软件。

这个小工具主要是使用MouseInfo类实时获取鼠标的信息,然后再JDialog上显示出来。

代码如下:


import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Point;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.Color;

public class MouseInfo extends JFrame {

    private final JPanel contentPanel = new JPanel();
    JLabel value_x = null;
    JLabel value_y = null;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            MouseInfo info_frame = new MouseInfo();
            info_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            info_frame.setVisible(true);
            info_frame.setAlwaysOnTop(true);
            Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    Point point = java.awt.MouseInfo.getPointerInfo().getLocation();
                    // System.out.println("Location:x=" + point.x + ", y=" +
                    // point.y);
                    info_frame.value_x.setText("" + point.x);
                    info_frame.value_y.setText("" + point.y);
                }
            }, 100, 100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public MouseInfo() {
        setTitle("\u9F20\u6807\u5750\u6807\u83B7\u53D6\u5668");
        setBounds(100, 100, 217, 156);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        contentPanel.setLayout(null);

        JLabel lblx = new JLabel("\u5750\u6807x:");
        lblx.setFont(new Font("宋体", Font.PLAIN, 15));
        lblx.setBounds(22, 27, 66, 31);
        contentPanel.add(lblx);

        JLabel lbly = new JLabel("\u5750\u6807y:");
        lbly.setFont(new Font("宋体", Font.PLAIN, 15));
        lbly.setBounds(22, 68, 66, 31);
        contentPanel.add(lbly);

        value_x = new JLabel("0");
        value_x.setForeground(Color.BLUE);
        value_x.setFont(new Font("宋体", Font.PLAIN, 20));
        value_x.setBounds(82, 27, 66, 31);
        contentPanel.add(value_x);

        value_y = new JLabel("0");
        value_y.setForeground(Color.BLUE);
        value_y.setFont(new Font("宋体", Font.PLAIN, 20));
        value_y.setBounds(82, 68, 66, 31);
        contentPanel.add(value_y);
    }
}

附上工具下载链接:http://download.csdn.net/detail/code_better/9808316

  • 13
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
可以使用Java中的AWT库中的`Robot`和`BufferedImage`类来实现。具体步骤如下: 1. 使用`Robot`类的`createScreenCapture()`方法获取屏幕截图,将截图保存为`BufferedImage`对象。 2. 使用`Robot`类的`mouseMove()`方法移动鼠标到大图中的某个位置。 3. 使用`Robot`类的`createScreenCapture()`方法获取该位置周围一定范围内的屏幕截图,将截图保存为`BufferedImage`对象。 4. 使用Java图像处理库如OpenCV对大图和小图进行匹配,找出小图在大图中的位置坐标。 下面是示例代码: ``` import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; public class ImageLocator { public static void main(String[] args) throws Exception { // 小图路径 File smallImageFile = new File("small.png"); // 大图路径 File largeImageFile = new File("large.png"); // 加载小图和大图为BufferedImage对象 BufferedImage smallImage = ImageIO.read(smallImageFile); BufferedImage largeImage = ImageIO.read(largeImageFile); // 创建Robot对象 Robot robot = new Robot(); // 获取屏幕截图 Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()); BufferedImage screenImage = robot.createScreenCapture(screenRect); // 移动鼠标到大图中的某个位置 int x = 100, y = 200; robot.mouseMove(x, y); // 获取该位置周围一定范围内的屏幕截图 int width = smallImage.getWidth(); int height = smallImage.getHeight(); Rectangle captureRect = new Rectangle(x - width/2, y - height/2, width*2, height*2); BufferedImage captureImage = robot.createScreenCapture(captureRect); // 在大图中查找小图的位置 // 这里使用了OpenCV库的Java接口,需要先安装OpenCV并导入相关库 // 如果不想使用OpenCV,可以使用Java自带的图像处理库,如Java Advanced Imaging (JAI) Point matchPoint = findMatchPoint(largeImage, captureImage, smallImage); // 输出匹配结果 System.out.println("Small image found at (" + (matchPoint.x - width/2) + ", " + (matchPoint.y - height/2) + ")"); } // 在大图中查找小图的位置 private static Point findMatchPoint(BufferedImage largeImage, BufferedImage captureImage, BufferedImage smallImage) { // 这里使用了OpenCV库的Java接口,需要先安装OpenCV并导入相关库 // 如果不想使用OpenCV,可以使用Java自带的图像处理库,如Java Advanced Imaging (JAI) // 下面示例代码仅供参考,具体实现方法可根据需要自行修改 Mat largeMat = bufferedImageToMat(largeImage); Mat captureMat = bufferedImageToMat(captureImage); Mat smallMat = bufferedImageToMat(smallImage); Mat result = new Mat(); Imgproc.matchTemplate(largeMat, smallMat, result, Imgproc.TM_CCOEFF_NORMED); Core.MinMaxLocResult mmr = Core.minMaxLoc(result); Point matchPoint = mmr.maxLoc; return matchPoint; } // 将BufferedImage对象转换为OpenCV中的Mat对象 private static Mat bufferedImageToMat(BufferedImage image) { byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3); mat.put(0, 0, pixels); return mat; } } ``` 注意:上述代码仅供参考,具体实现方法可根据需要自行修改。另外,使用`Robot`类获取屏幕截图和移动鼠标可能会影响用户体验,建议使用时谨慎。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值