Java模拟QQ桌面截图功能实现方法

 更新时间:2015年07月20日 10:19:37   作者:鉴客  
这篇文章主要介绍了Java模拟QQ桌面截图功能实现方法,涉及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程序设计有所帮助。

相关文章

  • java利用jacob将word转pdf

    java利用jacob将word转pdf

    这篇文章主要为大家详细介绍了java利用jacob将word转pdf,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-12-12
  • spring cloud gateway网关路由分配代码实例解析

    spring cloud gateway网关路由分配代码实例解析

    这篇文章主要介绍了spring cloud gateway网关路由分配代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-01-01
  • Spring Boot 集成 MyBatis 全面讲解(最新推荐)

    Spring Boot 集成 MyBatis 全面讲解(最新推荐)

    MyBatis 是一款优秀的持久层框架,与 Spring Boot 集成后可以大大简化开发流程,本文将全面讲解如何在 Spring Boot 中集成 MyBatis,包括环境配置、基础操作、高级功能和最佳实践,需要的朋友可以参考下
    2024-12-12
  • java list常用方法总结

    java list常用方法总结

    这篇文章主要介绍了java list常用方法总结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-11-11
  • 一文教会你如何搭建vue+springboot项目

    一文教会你如何搭建vue+springboot项目

    最近在捣鼓 SpringBoot 与 Vue 整合的项目,所以下面这篇文章主要给大家介绍了关于如何通过一篇文章教会你搭建vue+springboot项目,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-05-05
  • 用Java将字符串的首字母转换大小写

    用Java将字符串的首字母转换大小写

    在项目开发的时候会需要统一字符串的格式,比如首字母要求统一大写或小写,那用Java如何实现这一功能?下面一起来学习学习。
    2016-08-08
  • springboot 中整合mybatis多数据源不使用JPA

    springboot 中整合mybatis多数据源不使用JPA

    这篇文章主要介绍了springboot 中整合mybatis多数据源不使用JPA,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Kryo序列化及反序列化用法示例

    Kryo序列化及反序列化用法示例

    这篇文章主要介绍了Kryo序列化及反序列化用法示例,小编觉得挺不错的,这里分享给大家,需要的朋友可以参考下。
    2017-10-10
  • Java基于jeeplus vue实现简单工作流过程图解

    Java基于jeeplus vue实现简单工作流过程图解

    这篇文章主要介绍了Java基于jeeplus vue实现简单工作流过程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Spring容器-BeanFactory和ApplicationContext使用详解

    Spring容器-BeanFactory和ApplicationContext使用详解

    这篇文章主要为大家介绍了Spring容器-BeanFactory和ApplicationContext的使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04

最新评论