Java截图.docx
- 文档编号:4468304
- 上传时间:2022-12-01
- 格式:DOCX
- 页数:22
- 大小:161.69KB
Java截图.docx
《Java截图.docx》由会员分享,可在线阅读,更多相关《Java截图.docx(22页珍藏版)》请在冰豆网上搜索。
Java截图
源代码:
packagetest;
importjava.awt.Container;
importjava.awt.FlowLayout;
importjava.awt.GraphicsDevice;
importjava.awt.GraphicsEnvironment;
importjava.awt.Rectangle;
importjava.awt.Robot;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.JButton;
importjavax.swing.JFileChooser;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
importjavax.swing.UIManager;
importjavax.swing.filechooser.FileFilter;
importjavax.swing.filechooser.FileNameExtensionFilter;
importjava.awt.AlphaComposite;
importjava.awt.Graphics2D;
importjava.awt.BorderLayout;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Toolkit;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.event.MouseMotionListener;
importjava.awt.image.BufferedImage;
importjavax.swing.JTextArea;
importjavax.swing.JWindow;
importjava.awt.Dimension;
importjava.awt.Graphics;
importjava.awt.Image;
importjavax.swing.ImageIcon;
importjavax.swing.JLabel;
importjavax.swing.JPanel;
/**
*
*@authorGJ
*/
classCaptureViewextendsJWindowimplementsMouseListener,KeyListener,MouseMotionListener{
privatestaticfinallongserialVersionUID=1L;
privateBufferedImagedesktopImg;
privatebooleancaptured=false,draging=false,toolPanelAtRight=true;
privateintx=0,y=0,x1=0,y1=0,x2=1,y2=1;//坐标原点坐标,选区左上角和右下角坐标
privateintpoint_x,point_y;//鼠标点坐标
privateColorpoint_color;//鼠标点颜色
privateDesktopCapturewindow;//所属截图工具主窗体
privateImagePaneltoolPanel;//提示整框
privatefinalintTOOLPANEL_WIDTH=200,TOOLPANEL_HEIGHT=300,HALF_PICK_IMG=40;
privateJTextAreainfoArea;//提示区
privateToolImagePanelpickImgPanel;//放大镜
CaptureView(DesktopCapturewindow,BufferedImageimg){
super(window);
this.window=window;
this.desktopImg=img;
setSize(Toolkit.getDefaultToolkit().getScreenSize());
init();
setVisible(true);
setAlwaysOnTop(true);
this.requestFocus();
}
voidinit(){
this.setContentPane(newBackGroundPanel(desktopImg));
setLayout(null);
toolPanel=newImagePanel();
toolPanel.setLayout(newBorderLayout());
pickImgPanel=newToolImagePanel();
infoArea=newJTextArea();
infoArea.setOpaque(false);
infoArea.setEditable(false);
infoArea.setForeground(Color.BLUE);
infoArea.setFont(newFont("楷体",Font.PLAIN,11));
infoArea.setText("");
toolPanel.add(pickImgPanel,BorderLayout.CENTER);
toolPanel.add(infoArea,BorderLayout.SOUTH);
toolPanel.setLocation(getWidth()-TOOLPANEL_WIDTH,0);
toolPanel.setSize(TOOLPANEL_WIDTH,TOOLPANEL_HEIGHT);
this.getLayeredPane().add(toolPanel,300);
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
}
//桌面屏幕更新
publicvoidrefreshBackGround(BufferedImageimg){
this.desktopImg=img;
this.setContentPane(newBackGroundPanel(desktopImg));
setVisible(true);
setAlwaysOnTop(true);
this.requestFocus();
}
publicvoidpaint(Graphicsg){
super.paint(g);
g.setColor(Color.BLACK);
if(captured==true){
if(draging){
//截图辅助十字线
g.drawLine(point_x,0,point_x,getHeight());
g.drawLine(0,point_y,getWidth(),point_y);
}
confirmArea();//确定截图选区的左上角坐标(x1,y1)和右下角坐标(x2,y2)
if(x1 g.drawImage(desktopImg.getSubimage(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1)),x1,y1,null); g.drawRect(x1,y1,Math.abs(x2-x1),Math.abs(y2-y1)); }else{ g.drawLine(point_x,0,point_x,getHeight()); g.drawLine(0,point_y,getWidth(),point_y); } repaintToolPanel(); } //确定提示框位置并重画 publicvoidrepaintToolPanel(){ if(toolPanelAtRight==true){ if(point_x>(getWidth()-TOOLPANEL_WIDTH-100)&&point_y<(TOOLPANEL_HEIGHT+100)){ toolPanel.setLocation(0,0); toolPanelAtRight=false; } }else{ if(point_x<(TOOLPANEL_WIDTH+100)&&point_y<(TOOLPANEL_HEIGHT+100)){ toolPanel.setLocation(getWidth()-TOOLPANEL_WIDTH,0); toolPanelAtRight=true; } } //计算point的状态 point_color=newColor(desktopImg.getRGB(point_x,point_y)); refreshInfoText();//更新提示框内容 } //提示框状态信息的刷新 publicvoidrefreshInfoText(){ //文本信息 Stringtext=newString("1.单击截图------>进入截图\n2.双击右键------>退出截图\n3.双击左键------>保存截图\n4.单击右键------>重新截图"); StringinfoString; intcaptureWidth,captureHeight; if(captured==true){ captureWidth=x2-x; captureHeight=y2-y; }else{ captureWidth=0; captureHeight=0; } infoString="X,Y: "+point_x+","+point_y+"W*H: "+captureWidth+"*"+captureHeight+ "\nRBG: ("+point_color.getRed()+","+point_color.getGreen()+","+point_color.getBlue()+")\n"+text; infoArea.setText(infoString); //放大镜信息 intpick_x1,pick_y1,pick_x2,pick_y2,pickImg_x,pickImg_y; if(point_x-HALF_PICK_IMG<0){//获得放大图片的捡取左上角和右下角坐标,以及在放大镜中的左上角位置坐标 pick_x1=0; pick_x2=point_x+HALF_PICK_IMG; pickImg_x=HALF_PICK_IMG-point_x; }elseif(point_x+HALF_PICK_IMG>this.getWidth()){ pick_x1=point_x-HALF_PICK_IMG; pick_x2=this.getWidth(); pickImg_x=0; }else{ pick_x1=point_x-HALF_PICK_IMG; pick_x2=point_x+HALF_PICK_IMG; pickImg_x=0; } if(point_y-HALF_PICK_IMG<0){ pick_y1=0; pick_y2=point_y+HALF_PICK_IMG; pickImg_y=HALF_PICK_IMG-point_y; }elseif(point_y+HALF_PICK_IMG>this.getHeight()){ pick_y1=point_y-HALF_PICK_IMG; pick_y2=this.getHeight(); pickImg_y=0; }else{ pick_y1=point_y-HALF_PICK_IMG; pick_y2=point_y+HALF_PICK_IMG; pickImg_y=0; } BufferedImagepickImg=newBufferedImage(HALF_PICK_IMG*2,HALF_PICK_IMG*2,BufferedImage.TYPE_INT_RGB); GraphicspickGraphics=pickImg.getGraphics(); pickGraphics.drawImage(desktopImg.getSubimage(pick_x1,pick_y1,pick_x2-pick_x1,pick_y2-pick_y1),pickImg_x,pickImg_y,Color.black,null); pickImgPanel.refreshImg(pickImg.getScaledInstance(TOOLPANEL_WIDTH,TOOLPANEL_WIDTH,BufferedImage.SCALE_AREA_AVERAGING)); toolPanel.validate(); } //确定区域的左上点和右下角坐标 publicvoidconfirmArea(){ inttemp; //以x,y为截图选区左上角坐标初值,计算左上角x1,y1和右下角x2,y2的坐标 x1=x; y1=y; if(x2 if(y2 temp=x1; x1=x2; x2=temp; temp=y1; y1=y2; y2=temp; }else{//4 temp=x1; x1=x2; x2=temp; } }else{//1,4 if(y2 temp=y1; y1=y2; y2=temp; } } } publicvoidexit(){ x=0; y=0; x1=0; y1=0; x2=1; y2=1; point_x=0; point_y=0; captured=false; draging=false; toolPanel.setLocation(getWidth()-TOOLPANEL_WIDTH,0); toolPanelAtRight=true; this.setVisible(false); //this.dispose(); } @Override publicvoidmouseClicked(MouseEvente){ //TODOAuto-generatedmethodstub //双击左键,确定选择图像选区,保存截图 if(e.getButton()==MouseEvent.BUTTON1){ if(e.getClickCount()==2){ this.setVisible(false); window.toFront(); window.saveCapture(x1,y1,x2,y2); if(! window.iconed)window.setVisible(true); exit(); } }elseif(e.getClickCount()==2){ if(! window.iconed){ window.setVisible(true); window.toFront(); } exit(); } } @Override publicvoidmouseEntered(MouseEvente){ //TODOAuto-generatedmethodstub } @Override publicvoidmouseExited(MouseEvente){ //TODOAuto-generatedmethodstub } @Override publicvoidmousePressed(MouseEvente){ //TODOAuto-generatedmethodstub //左键在未选区完毕时单击,确定选区起点 if(e.getButton()==MouseEvent.BUTTON1){ if(captured==false){ point_x=e.getX(); point_y=e.getY(); x=point_x;//选区起点坐标 y=point_y; draging=true; captured=true; } } } @Override publicvoidmouseReleased(MouseEvente){ //TODOAuto-generatedmethodstub //左键释放,确定截图选区 //右键释放,重新选区 if(e.getButton()==MouseEvent.BUTTON1){ if(draging==true){ point_x=e.getX(); point_y=e.getY(); x2=point_x; y2=point_y; repaint(); draging=false; } }else{ draging=false; captured=false; point_x=e.getX(); point_y=e.getY(); repaint(); } } @Override publicvoidmouseDragged(MouseEvente){ //TODOAuto-generatedmethodstub //拖拉截图时画矩形 if(draging==true){ point_x=e.getX(); point_y=e.getY(); x2=point_x; y2=point_y; repaint(); } } @Override publicvoidmouseMoved(MouseEvente){ //TODOAuto-generatedmethodstub //未决定截图时,画交叉线 if(! captured){ point_x=e.getX(); point_y=e.getY(); repaint(); } } @Override publicvoidkeyPressed(KeyEvente){ //TODOAuto-generatedmethodstub if(e.getKeyCode()==KeyEvent.VK_CANCEL){ this.setVisible(false); this.dispose(); } } @Override publicvoidkeyReleased(KeyEvente){ //TODOAuto-generatedmethodstub System.out.println("aaa"); if(e.getKeyCode()==KeyEvent.VK_A){ this.setVisible(false); this.dispose(); } } @Override publicvoidkeyTyped(KeyEvente){ //TODOAuto-generatedmethodstub if(e.getKeyCode()==KeyEvent.VK_ESCAPE){ this.setVisible(false); this.dispose(); } } } classBackGroundPanelextendsImagePanel{ privatestaticfinallongserialVersionUID=1L; intwidth,height; publicBackGroundPanel(){ super(); } publicBackGroundPanel(StringimgUrl){ super(imgUrl); } publicBackGroundPanel(Imageimg){ super(img); } publicvoidrefreshImg(Im
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 截图