Java模拟QQ桌面截图功能实现方法
作者:鉴客 发布时间:2021-09-19 16:30:02
标签:Java,截图
本文实例讲述了Java模拟QQ桌面截图功能实现方法。分享给大家供大家参考。具体如下:
QQ的桌面截图功能非常方便,去年曾用Java模拟过一个,现整理出来。
本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示,这样就模拟出了一个桌面,Java也就可以获得鼠标的作用区域从而实现桌面中的小范围截屏。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
/**
* 用Java模拟出QQ桌面截图功能
*/
public class Test extends JFrame {
private static final long serialVersionUID = -267804510087895906L;
private JButton button = null;
private JLabel imgLabel = null;
public Test() {
button = new JButton("模拟屏幕(点右键退出)");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
new ScreenWindow(imgLabel);
} catch (Exception e1) {
JOptionPane.showConfirmDialog(null, "出现意外错误!", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
}
});
JPanel pane = new JPanel();
pane.setBackground(Color.WHITE);
imgLabel = new JLabel();
pane.add(imgLabel);
JScrollPane spane = new JScrollPane(pane);
this.getContentPane().add(button, BorderLayout.NORTH);
this.getContentPane().add(spane);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(300, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Test();
}
}
class ScreenWindow extends JFrame {
private static final long serialVersionUID = -3758062802950480258L;
private boolean isDrag = false;
private int x = 0;
private int y = 0;
private int xEnd = 0;
private int yEnd = 0;
public ScreenWindow(final JLabel imgLabel) throws AWTException, InterruptedException {
Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0, 0, screenDims.width, screenDims.height)));
label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
label.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
dispose();
}
}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e) {
if (isDrag) {
xEnd = e.getX();
yEnd = e.getY();
if(x > xEnd){
int temp = x;
x = xEnd;
xEnd = temp;
}
if(y > yEnd){
int temp = y;
y = yEnd;
yEnd = temp;
}
try {
imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x, y, xEnd - x, yEnd - y)));
} catch (Exception ex) {
JOptionPane.showConfirmDialog(null, "出现意外错误!", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
}
dispose();
}
}
});
label.addMouseMotionListener(new MouseMotionListener() {
public void mouseDragged(MouseEvent e) {
if(!isDrag)
isDrag = true;
}
public void mouseMoved(MouseEvent e) {
/** 拖动过程的虚线选取框需自己实现 */
}
});
this.setUndecorated(true);
this.getContentPane().add(label);
this.setSize(screenDims.width, screenDims.height);
this.setVisible(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
class ScreenImage {
public static Image getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {
Robot robot = new Robot();
Image screen = robot.createScreenCapture(new Rectangle(x, y, w, h)).getScaledInstance(w, h, Image.SCALE_SMOOTH);
MediaTracker tracker = new MediaTracker(new Label());
tracker.addImage(screen, 1);
tracker.waitForID(0);
return screen;
}
}
希望本文所述对大家的java程序设计有所帮助。


猜你喜欢
- 背景SpringBoot 版本<parent> <groupId>org.springfr
- 本文实例讲述了C语言二叉树常见操作。分享给大家供大家参考,具体如下:一、基本概念每个结点最多有两棵子树,左子树和右子树,次序不可以颠倒。性质
- 要将一个对象序列化,可是如果对象的属性为null的时候,我们想将属性为null的都去掉。在这里我使用Newtonsoft.Json.dll记
- 前言《英文猜词游戏》代码行数没有超过200行,是之前为了背英语单词,特意研发的小游戏。主要设计1.事先准备单词文本。2.为了让玩家能与程序互
- 1、return语句的作用:a、返回一个值,这个值可以是任意类型。b、使程序返回到操作系统(即终止程序)2、java中对于一个函数,不论有没
- SSM@Controller,@Service本质都是@Component,作用是new对象放到Spring容器里。controller层@
- 本文实例讲述了spring AOP的Around增强实现方法。分享给大家供大家参考,具体如下:一 配置<?xml version=&q
- 一、AOP概述AOP,即面向切面编程,简单来说就是将代码中重复的部分抽取出来,在需要执行的时候使用 * 的技术,在不修改源码的基础上对方法
- 将SpringBoot项目部署到腾讯云注意:1、如果已经下载好MySql和JDK,可以直接跳过1、3步骤。但是不要忘记步骤2哦。2、如果已经
- 这篇文章说明了如何通过Maven配置Spring依赖项。最新的Spring版本可以在Maven Central上找到。Maven中的Spri
- 在使用ComboBox控件时,遇到了重新绑定赋值出问题的情况。正常情况下,对于数据重新赋值的或者绑定数据源的时候,为了防止数据出现问题,都会
- 前段时间写了一篇基于mybatis实现的多数据源博客。感觉不是很好,这次打算加入git,来搭建一个基于Mybatis-Plus的多数据源项目
- 代理模式:为其他对象提供一种代理以控制某个对象的访问。用在:在某些情况下,一个客户不想或者不能直接访问另一个对象,而代理对象可以在客户端和目
- 最近在折腾一些控制相关的软件设计,想起来状态机这个东西,对解决一些控制系统状态切换还是挺有用的。状态机(有限状态自动机)网上有很多介绍。简单
- 前言在日常的测试工作过程中,app可能会出现闪退崩溃的情况,这个时候就需要测试同学快速抓取到崩溃日志,来有效的辅助开发定位问题,快速的去解决
- 如下所示:import java.util.*;public class Main{public static void main(Stri
- 1.基本语法key: value;kv之间有空格大小写敏感使用缩进表示层级关系缩进不允许使用tab,只允许空格缩进的空格数不重要,只要相同层
- 本文实例讲述了C#实现TCP连接信息统计的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.
- 跨域配置如下,Springboot 版本为 2.4.1///跨域访问配置@Configurationpublic class CorsCon
- 本文实例讲述了Android编程之View简单学习示例。分享给大家供大家参考,具体如下:View,是Android的一个超类,这个类几乎包含