Java模拟QQ桌面截图功能实现方法
作者:鉴客 时间:2021-09-19 16:30:02
本文实例讲述了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程序设计有所帮助。
标签:Java,截图
0
投稿
猜你喜欢
SpringCloud实战小贴士之Zuul的路径匹配
2021-05-26 13:26:36
散列表的原理与Java实现方法详解
2023-06-06 06:46:29
Java调用明华RF读写器DLL文件过程解析
2022-12-17 04:31:06
C#开启线程的四种示例
2022-06-01 13:04:37
关于@GetMapping和@GetMapping(value=““)的区别
2023-11-27 11:07:37
Java MongoDB数据库连接方法梳理
2023-11-25 01:01:20
由浅到深带你详谈Java实现数组扩容的三种方式
2023-07-05 18:37:19
spring boot org.junit.jupiter.api不存在的解决
2023-07-11 18:34:16
C#的并发机制优秀在哪你知道么
2022-11-09 09:44:35
C#验证给定字符串形式日期是否合法的方法
2021-09-15 16:00:37
举例讲解设计模式中的访问者模式在Java编程中的运用
2022-04-04 08:00:45
Android开发实现控件双击事件的监听接口封装类
2023-02-15 00:56:18
Mybatis generator如何自动生成代码
2023-08-13 10:15:15
java JUC信号量Semaphore原理及使用介绍
2023-01-02 02:08:57
JavaScript嵌入百度地图API的最详细方法
2023-04-12 14:33:03
Java中Prime算法的原理与实现详解
2022-06-11 23:16:29
SpringBoot万字爆肝高级配置
2022-09-17 06:34:08
C#使用ILGenerator动态生成函数的简单代码
2022-07-10 01:41:06
使用java反射将结果集封装成为对象和对象集合操作
2022-03-11 18:30:26
C#中ref和out的区别浅析
2021-12-22 23:00:50