Java模拟QQ桌面截图功能实现方法
本文实例讲述了Java模拟QQ桌面截图功能实现方法。分享给大家供大家参考。具体如下:
QQ的桌面截图功能非常方便,去年曾用Java模拟过一个,现整理出来。
本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示,这样就模拟出了一个桌面,Java也就可以获得鼠标的作用区域从而实现桌面中的小范围截屏。
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.MouseAdapter;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseMotionListener;
/**
*用Java模拟出QQ桌面截图功能
*/
publicclassTestextendsJFrame{
privatestaticfinallongserialVersionUID=-267804510087895906L;
privateJButtonbutton=null;
privateJLabelimgLabel=null;
publicTest(){
button=newJButton("模拟屏幕(点右键退出)");
button.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
try{
newScreenWindow(imgLabel);
}catch(Exceptione1){
JOptionPane.showConfirmDialog(null,"出现意外错误!","系统提示",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE);
}
}
});
JPanelpane=newJPanel();
pane.setBackground(Color.WHITE);
imgLabel=newJLabel();
pane.add(imgLabel);
JScrollPanespane=newJScrollPane(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);
}
publicstaticvoidmain(String[]args){
newTest();
}
}
classScreenWindowextendsJFrame{
privatestaticfinallongserialVersionUID=-3758062802950480258L;
privatebooleanisDrag=false;
privateintx=0;
privateinty=0;
privateintxEnd=0;
privateintyEnd=0;
publicScreenWindow(finalJLabelimgLabel)throwsAWTException,InterruptedException{
DimensionscreenDims=Toolkit.getDefaultToolkit().getScreenSize();
JLabellabel=newJLabel(newImageIcon(ScreenImage.getScreenImage(0,0,screenDims.width,screenDims.height)));
label.setCursor(newCursor(Cursor.CROSSHAIR_CURSOR));
label.addMouseListener(newMouseAdapter(){
publicvoidmouseClicked(MouseEvente){
if(e.getButton()==MouseEvent.BUTTON3){
dispose();
}
}
publicvoidmousePressed(MouseEvente){
x=e.getX();
y=e.getY();
}
publicvoidmouseReleased(MouseEvente){
if(isDrag){
xEnd=e.getX();
yEnd=e.getY();
if(x>xEnd){
inttemp=x;
x=xEnd;
xEnd=temp;
}
if(y>yEnd){
inttemp=y;
y=yEnd;
yEnd=temp;
}
try{
imgLabel.setIcon(newImageIcon(ScreenImage.getScreenImage(x,y,xEnd-x,yEnd-y)));
}catch(Exceptionex){
JOptionPane.showConfirmDialog(null,"出现意外错误!","系统提示",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE);
}
dispose();
}
}
});
label.addMouseMotionListener(newMouseMotionListener(){
publicvoidmouseDragged(MouseEvente){
if(!isDrag)
isDrag=true;
}
publicvoidmouseMoved(MouseEvente){
/**拖动过程的虚线选取框需自己实现*/
}
});
this.setUndecorated(true);
this.getContentPane().add(label);
this.setSize(screenDims.width,screenDims.height);
this.setVisible(true);
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
}
}
classScreenImage{
publicstaticImagegetScreenImage(intx,inty,intw,inth)throwsAWTException,InterruptedException{
Robotrobot=newRobot();
Imagescreen=robot.createScreenCapture(newRectangle(x,y,w,h)).getScaledInstance(w,h,Image.SCALE_SMOOTH);
MediaTrackertracker=newMediaTracker(newLabel());
tracker.addImage(screen,1);
tracker.waitForID(0);
returnscreen;
}
}
希望本文所述对大家的java程序设计有所帮助。