一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java模擬QQ桌面截圖功能實現方法

Java模擬QQ桌面截圖功能實現方法

2019-12-27 12:52鑒客 JAVA教程

這篇文章主要介紹了Java模擬QQ桌面截圖功能實現方法,涉及java針對鼠標事件及圖片操作的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下

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

QQ的桌面截圖功能非常方便,去年曾用Java模擬過一個,現整理出來。
本方法首先需要抓到屏幕的整個圖象,將圖象顯示在一個JFrame中,再將JFrame全屏顯示,這樣就模擬出了一個桌面,Java也就可以獲得鼠標的作用區域從而實現桌面中的小范圍截屏。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩精品免费一区二区 | 四虎影院的网址 | 91日本在线观看亚洲精品 | 国内会所按摩推拿国产 | 四虎影院新地址 | 国产精品免费精品自在线观看 | 欧美日韩国产超高清免费看片 | 青春草在线观看精品免费视频 | aaaa大片| 国产普通话对白露脸流出 | 久久丫线这里只精品 | 男人疯狂擦进女人下面 | 国产亚洲精品一区二区在线播放 | 国产一区二区视频在线播放 | 亚洲国产欧美在线人成aaa | gay台湾无套男同志可播放 | 日韩欧美亚洲国产高清在线 | 亚洲福利 影院 | 日韩制服丝袜在线观看 | 精品国语国产在线对白 | 成人在线小视频 | 国产成人精品视频频 | 无人区在线观看免费视频国语 | a亚洲天堂 | 亚洲国产在线观看免费视频 | 疯狂激吻添下边小说 | 欧美亚洲综合另类 | nxgx在线观看国产中文 | 国内精品久久久久影院中国 | 日本在线观看www鲁啊鲁视频 | 国产小视频网站 | 波多野结衣 在线 | 国产小视频在线播放 | 99在线精品日韩一区免费国产 | 欧美一级特黄aaa大片 | 日本高清视频一区二区 | 亚洲天堂视频在线观看免费 | 国产精品自在线 | 亚洲第一免费播放区 | 超级乱淫寡妇 | 2019国内自拍大神视频 |