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
投稿

猜你喜欢

  • C#重载运算符详解

    2023-10-27 13:35:18
  • Java多线程常见案例分析线程池与单例模式及阻塞队列

    2022-06-01 06:42:42
  • JAVA实现基于Tcp协议的简单Socket通信实例

    2022-07-07 21:44:36
  • 详解Dagger2在Android开发中的新用法

    2021-08-23 22:39:40
  • JavaEE中struts2实现文件上传下载功能实例解析

    2023-03-09 07:54:31
  • Java农夫过河问题的继承与多态实现详解

    2022-07-12 16:13:22
  • Java基础入门总结之序列化和反序列化

    2023-02-19 03:29:10
  • Java 8中Stream API的这些奇技淫巧!你Get了吗?

    2023-04-20 20:31:44
  • C#编译器对局部变量的优化指南

    2023-06-23 14:54:12
  • idea中同一SpringBoot项目多端口启动

    2023-05-09 06:39:27
  • Java设计模式之职责链模式详解

    2023-01-26 21:16:26
  • myEclipse配置jdk1.7教程

    2022-07-21 11:25:35
  • Java方法重载Overload原理及使用解析

    2021-11-21 14:23:14
  • Android本地实现搜索历史记录

    2023-08-15 22:25:17
  • 详解Java对象的强、软、弱和虚引用+ReferenceQueue

    2021-11-30 16:23:01
  • Android填坑系列:在小米系列等机型上放开定位权限后的定位请求弹框示例

    2022-03-29 15:15:49
  • Java如何自定义异常打印非堆栈信息详解

    2022-05-06 09:43:36
  • 基数排序简介及Java语言实现

    2021-10-06 01:15:13
  • java输入数字,输出倒序的实例

    2023-06-20 01:59:44
  • 浅谈SpringBoot @Autowired的两种注入方式

    2021-06-28 06:08:34
  • asp之家 软件编程 m.aspxhome.com