欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 18:27:43
本文实例讲述了java模拟qq桌面截图功能实现方法。分享给大家供大家参考。具体如下: 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程序设计有所帮助。