欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java模擬QQ桌面截圖功能實現(xiàn)方法

 更新時間:2015年07月20日 10:19:37   作者:鑒客  
這篇文章主要介紹了Java模擬QQ桌面截圖功能實現(xiàn)方法,涉及java針對鼠標(biāo)事件及圖片操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Java模擬QQ桌面截圖功能實現(xiàn)方法。分享給大家供大家參考。具體如下:

QQ的桌面截圖功能非常方便,去年曾用Java模擬過一個,現(xiàn)整理出來。
本方法首先需要抓到屏幕的整個圖象,將圖象顯示在一個JFrame中,再將JFrame全屏顯示,這樣就模擬出了一個桌面,Java也就可以獲得鼠標(biāo)的作用區(qū)域從而實現(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("模擬屏幕(點右鍵退出)");
 button.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {
  try {
   new ScreenWindow(imgLabel);
  } catch (Exception e1) {
   JOptionPane.showConfirmDialog(null, "出現(xiàn)意外錯誤!", "系統(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)意外錯誤!", "系統(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) {
  /** 拖動過程的虛線選取框需自己實現(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;
 }
}

希望本文所述對大家的java程序設(shè)計有所幫助。

相關(guān)文章

  • 徹底搞懂java并發(fā)ThreadPoolExecutor使用

    徹底搞懂java并發(fā)ThreadPoolExecutor使用

    這篇文章主要為大家介紹了徹底搞懂java并發(fā)ThreadPoolExecutor使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-02-02
  • Mybatis動態(tài)拼接sql提高插入速度實例

    Mybatis動態(tài)拼接sql提高插入速度實例

    這篇文章主要介紹了Mybatis動態(tài)拼接sql提高插入速度實例,當(dāng)數(shù)據(jù)量少的時候,沒問題,有效時間內(nèi)可能完成插入,但是當(dāng)數(shù)據(jù)量達到一定程度的時候,每次都一個sql插入超時,所以采用了拼接sql的方式加快速度,需要的朋友可以參考下
    2023-09-09
  • Ubuntu下配置Tomcat服務(wù)器以及設(shè)置自動啟動的方法

    Ubuntu下配置Tomcat服務(wù)器以及設(shè)置自動啟動的方法

    這篇文章主要介紹了Ubuntu下配置Tomcat服務(wù)器以及設(shè)置自動啟動的方法,適用于Java的web程序開發(fā),需要的朋友可以參考下
    2015-10-10
  • 使用@ConditionalOnProperty控制是否加載的操作

    使用@ConditionalOnProperty控制是否加載的操作

    這篇文章主要介紹了使用@ConditionalOnProperty控制是否加載的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 剖析Java中在Collection集合中使用contains和remove為什么要重寫equals

    剖析Java中在Collection集合中使用contains和remove為什么要重寫equals

    這篇文章主要介紹了Collection集合的contains和remove方法詳解remove以及相關(guān)的經(jīng)驗技巧,通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • java?socket實現(xiàn)局域網(wǎng)聊天

    java?socket實現(xiàn)局域網(wǎng)聊天

    這篇文章主要為大家詳細介紹了java?socket實現(xiàn)局域網(wǎng)聊天,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • SpringMVC執(zhí)行過程詳細講解

    SpringMVC執(zhí)行過程詳細講解

    MVC是一種軟件設(shè)計典范,用一種業(yè)務(wù)邏輯、數(shù)據(jù)、界面顯示分離的方法組織代碼,將業(yè)務(wù)邏輯聚集到一個組件里面,在改進和個性化定制界面及用戶交互的同時,不需要重新編寫業(yè)務(wù)邏輯,MVC分層有助于管理和架構(gòu)復(fù)雜的應(yīng)用程序
    2022-08-08
  • Spring?Boot常用注解(經(jīng)典干貨)

    Spring?Boot常用注解(經(jīng)典干貨)

    Spring?Boot是一個快速開發(fā)框架,快速的將一些常用的第三方依賴整合,全部采用注解形式,內(nèi)置Http服務(wù)器,最終以Java應(yīng)用程序進行執(zhí)行,這篇文章主要介紹了Spring?Boot常用注解(絕對經(jīng)典),需要的朋友可以參考下
    2023-01-01
  • SpringBoot對數(shù)據(jù)訪問層進行單元測試的方法詳解

    SpringBoot對數(shù)據(jù)訪問層進行單元測試的方法詳解

    我們公司作為一個面向銀行、金融機構(gòu)的TO B類企業(yè),頻繁遇到各個甲方爸爸提出的國產(chǎn)化數(shù)據(jù)庫的改造需求,包括OceanBase, TiDB,geldenDB等等,本文就介紹一種快高效、可復(fù)用的解決方案——對數(shù)據(jù)訪問層做單元測試,需要的朋友可以參考下
    2023-08-08
  • JDK1.8使用的垃圾回收器和執(zhí)行GC的時長以及GC的頻率方式

    JDK1.8使用的垃圾回收器和執(zhí)行GC的時長以及GC的頻率方式

    這篇文章主要介紹了JDK1.8使用的垃圾回收器和執(zhí)行GC的時長以及GC的頻率方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評論