欢迎来到天天文库
浏览记录
ID:40503521
大小:16.06 KB
页数:5页
时间:2019-08-03
《用Java实现屏幕截图》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、packagecom.qiu.util;importjava.io.*;importjava.net.*;importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;importjava.awt.image.*;importjavax.imageio.*;importjava.io.*;/**@AuthorQiu_BaiChao*一个简单的屏幕抓图***/publicclassScreenCapture{//testmainpublicsta
2、ticvoidmain(String[]args)throwsException{Stringuserdir=System.getProperty("user.dir");FiletempFile=newFile("d:","temp.png");ScreenCapturecapture=ScreenCapture.getInstance();capture.captureImage();JFrameframe=newJFrame();JPanelpanel=newJPanel();panel.set
3、Layout(newBorderLayout());JLabelimagebox=newJLabel();panel.add(BorderLayout.CENTER,imagebox);imagebox.setIcon(capture.getPickedIcon());capture.saveToFile(tempFile);capture.captureImage();imagebox.setIcon(capture.getPickedIcon());frame.setContentPane(pan
4、el);frame.setSize(400,300);frame.show();System.out.println("Over");}privateScreenCapture(){try{robot=newRobot();}catch(AWTExceptione){System.err.println("InternalError:"+e);e.printStackTrace();}JPanelcp=(JPanel)dialog.getContentPane();cp.setLayout(newBo
5、rderLayout());labFullScreenImage.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEventevn){isFirstPoint=true;pickedImage=fullScreenImage.getSubimage(recX,recY,recW,recH);dialog.setVisible(false);}});labFullScreenImage.addMouseMotionListe
6、ner(newMouseMotionAdapter(){publicvoidmouseDragged(MouseEventevn){if(isFirstPoint){x1=evn.getX();y1=evn.getY();isFirstPoint=false;}else{x2=evn.getX();y2=evn.getY();intmaxX=Math.max(x1,x2);intmaxY=Math.max(y1,y2);intminX=Math.min(x1,x2);intminY=Math.min(
7、y1,y2);recX=minX;recY=minY;recW=maxX-minX;recH=maxY-minY;labFullScreenImage.drawRectangle(recX,recY,recW,recH);}}publicvoidmouseMoved(MouseEvente){labFullScreenImage.drawCross(e.getX(),e.getY());}});cp.add(BorderLayout.CENTER,labFullScreenImage);dialog.
8、setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));dialog.setAlwaysOnTop(true);dialog.setMaximumSize(Toolkit.getDefaultToolkit().getScreenSize());dialog.setUndecorated(true);dialog.setSize(dialog.getMaximumSize());dia
此文档下载收益归作者所有