Java模擬QQ桌面截圖功能實(shí)現(xiàn)方法
本文實(shí)例講述了Java模擬QQ桌面截圖功能實(shí)現(xiàn)方法。分享給大家供大家參考。具體如下:
QQ的桌面截圖功能非常方便,去年曾用Java模擬過(guò)一個(gè),現(xiàn)整理出來(lái)。
本方法首先需要抓到屏幕的整個(gè)圖象,將圖象顯示在一個(gè)JFrame中,再將JFrame全屏顯示,這樣就模擬出了一個(gè)桌面,Java也就可以獲得鼠標(biāo)的作用區(qū)域從而實(shí)現(xiàn)桌面中的小范圍截屏。
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("模擬屏幕(點(diǎn)右鍵退出)");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
new ScreenWindow(imgLabel);
} catch (Exception e1) {
JOptionPane.showConfirmDialog(null, "出現(xiàn)意外錯(cuò)誤!", "系統(tǒng)提示", 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, "出現(xiàn)意外錯(cuò)誤!", "系統(tǒng)提示", 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) {
/** 拖動(dòng)過(guò)程的虛線(xiàn)選取框需自己實(shí)現(xiàn) */
}
});
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;
}
}
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
spring cloud gateway網(wǎng)關(guān)路由分配代碼實(shí)例解析
這篇文章主要介紹了spring cloud gateway網(wǎng)關(guān)路由分配代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01
Spring?Boot?集成?MyBatis?全面講解(最新推薦)
MyBatis 是一款優(yōu)秀的持久層框架,與 Spring Boot 集成后可以大大簡(jiǎn)化開(kāi)發(fā)流程,本文將全面講解如何在 Spring Boot 中集成 MyBatis,包括環(huán)境配置、基礎(chǔ)操作、高級(jí)功能和最佳實(shí)踐,需要的朋友可以參考下2024-12-12
一文教會(huì)你如何搭建vue+springboot項(xiàng)目
最近在搗鼓?SpringBoot?與?Vue?整合的項(xiàng)目,所以下面這篇文章主要給大家介紹了關(guān)于如何通過(guò)一篇文章教會(huì)你搭建vue+springboot項(xiàng)目,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05
用Java將字符串的首字母轉(zhuǎn)換大小寫(xiě)
在項(xiàng)目開(kāi)發(fā)的時(shí)候會(huì)需要統(tǒng)一字符串的格式,比如首字母要求統(tǒng)一大寫(xiě)或小寫(xiě),那用Java如何實(shí)現(xiàn)這一功能?下面一起來(lái)學(xué)習(xí)學(xué)習(xí)。2016-08-08
springboot 中整合mybatis多數(shù)據(jù)源不使用JPA
這篇文章主要介紹了springboot 中整合mybatis多數(shù)據(jù)源不使用JPA,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java基于jeeplus vue實(shí)現(xiàn)簡(jiǎn)單工作流過(guò)程圖解
這篇文章主要介紹了Java基于jeeplus vue實(shí)現(xiàn)簡(jiǎn)單工作流過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
Spring容器-BeanFactory和ApplicationContext使用詳解
這篇文章主要為大家介紹了Spring容器-BeanFactory和ApplicationContext的使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04

