java模拟QQ的截屏(代码)
javaÄ?ÄâQQµÄ?ØÆÁ(?úÂë)
package com.xfsuper.util.screenCamera;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage; import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class GuiCamera {
String fileName="GuiCamera";
String imageFormat="png";
int num=0;
Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
public GuiCamera()
{
}
public GuiCamera(String fileName,String imageFormat)
{
this.fileName=fileName;
this.imageFormat=imageFormat;
}
public void snpaShot(int startX,int startY,int endX,int endY)
{
try {
num++;
int width=startX>endX?startX-endX:endX-startX;
int height=startY>endY?startY-endY:endY-startY;
int temp;
if(startX>endX)
{
temp=startX;
startX=endX;
endX=temp;
}
if(startY>endY)
{
temp=startY;
startY=endY;
endY=temp;
}
// BufferedImage bImage=(new Robot()).createScreenCapture(new
Rectangle(0,0,(int)d.getWidth(),(int)d.getHeight()));
BufferedImage bImage=(new Robot()).createScreenCapture(new
Rectangle(startX,startY,width,height));
String name=fileName+String.valueOf(num)+"."+imageFormat;
File f=new File(name);
ImageIO.write(bImage,imageFormat,f);
} catch (AWTException e) {
// TODO ×Ô??Éú?É catch ?é
e.printStackTrace();
} catch (IOException e) {
// TODO ×Ô??Éú?É catch ?é
e.printStackTrace();
}
}
public static void main(String[] args) { // new GuiCamera("d:\\image","png").snpaShot();
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getImageFormat() {
return imageFormat;
}
public void setImageFormat(String imageFormat) {
this.imageFormat = imageFormat;
}
}
///////////////////////////////////////////////////////////////
package com.xfsuper.util.screenCamera; import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class ScreenFrame extends JFrame implements MouseMotionListener,
MouseListener{
JPanel jp;
Image Background;
Point p0;
Point p1;
int startx,starty,endx,endy;
public ScreenFrame() {
jp = new JPanel();
Toolkit tk =Toolkit.getDefaultToolkit();
Dimension dm = tk.getScreenSize();
this.getContentPane().add(jp);
try {
Robot r = new Robot();
Background = r.createScreenCapture(new Rectangle(0,0,(int)dm.getWidth(), (int)dm.getHeight()));
} catch(Exception e) {
//e.printStackTrace();
}
this.resize((int)dm.getWidth(), (int)dm.getHeight()+31);
//this.setSize(300,300);
this.setLocation(0,-30);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.setVisible(true);
}
public void paint(Graphics g) {
p0 = this.getLocationOnScreen();
p1 = new Point(-p0.x, -p0.y);
//draw background
g.drawImage(Background, p1.x, p1.y, null);
//draw Rect
int width=startx>endx?startx-endx:endx-startx;
int height=starty>endy?starty-endy:endy-starty;
if(endy>starty)
g.drawRect(startx+2,starty+31,width-4,height-31);
else
g.drawRect(endx,endy,width,height+31);
//Graphics2D g2d = (Graphics2D)g;
//repaint();
}
public static void main(String args[])
{
new ScreenFrame();
}
public void mouseClicked(MouseEvent e) {
if(e.getButton()==3)
{
System.exit(1);
}
}
public void mouseEntered(MouseEvent e) {
// TODO ×Ô??Éú?É?????æ?ù
}
public void mouseExited(MouseEvent e) {
// TODO ×Ô??Éú?É?????æ?ù
}
public void mousePressed(MouseEvent e) {
if(e.getButton()==1)
{
System.out.println(e.getY());
startx=e.getX()-4;
starty=e.getY()-31;
}
}
public void mouseReleased(MouseEvent e) {
//System.out.println(e.getY());
if(e.getButton()==1)
{
endx=e.getX();
endy=e.getY()-31;
System.out.println("startX="+startx+" starty="+starty+"
endx="+endx+" endy="+endy);
new
GuiCamera("d:\\image","png").snpaShot(startx,starty,endx,endy);
}
}
public void mouseDragged(MouseEvent e) {
//System.out.println("x="+e.getX()+" y="+e.getY());
endx=e.getX();
endy=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {
//System.out.println("x="+e.getX()+" y="+e.getY());
}
}
//////////////////////////////////////////
package com.xfsuper.util.screenCamera;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
public class MouseMovePoint extends JFrame implements
MouseMotionListener,MouseListener{
Container con=this.getContentPane();
public MouseMovePoint()
{
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.repaint();
this.setSize(500,500);
this.setResizable(false);
this.setVisible(true);
}
public void paint(Graphics g) {
//super.paintComponents(g);
g.setColor(Color.red);
g.drawLine(100,100,200,200);
}
public static void main(String[] args) {
new MouseMovePoint();
}
public void mouseDragged(MouseEvent e) {
System.out.println("x="+e.getX()+" y="+e.getY());
}
public void mouseMoved(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
System.out.println(e.getX());
}
public void mouseReleased(MouseEvent e) {
System.out.println(e.getX());
}
}