JAVA截屏程序(第四版)

昨天看到留言,于是今天又把一些功能加进进去了

比如,
1,程序可以最小化到系统托盘区
2,程序可以批量保存所有截过的图片
3,修正了以前的一个小BUG,那就是输入保存图片的文件名时,如果没有输入后缀名,将保存正常,如果输入了后缀名,将会保存格式出错
打包文件请 点击这里下载,dist文件夹里面的JAR文件是可以双击执行的

感谢朋友们的建议,谢谢.

/*
 * CaptureScreen.java
 *
 * Created on 2007年8月30日, 下午12:46
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 
*/

package  hadeslee.swing;

/**
 *
 * 
@author  lbf
 
*/
import  java.awt. * ;
import  java.awt.datatransfer.DataFlavor;
import  java.awt.datatransfer.Transferable;
import  java.awt.datatransfer.UnsupportedFlavorException;
import  java.awt.event. * ;
import  javax.swing. * ;
import  java.io. * ;
import  javax.imageio. * ;
import  java.awt.image. * ;

public   class  CaptureScreen  extends  JFrame  implements  ActionListener{
    
private  JButton start,cancel,saveAll;
    
private  JPanel c;
    
private  BufferedImage get;
    
private  JTabbedPane jtp; // 一个放置很多份图片
     private   int  index; // 一个一直会递增的索引,用于标认图片
     private  JRadioButton java,system; // JAVA界面,系统界面
     /**  Creates a new instance of CaptureScreen  */
    
public  CaptureScreen() {
        
super ( " 屏幕截取软件(第三版) " );
        
try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
catch (Exception exe){
            exe.printStackTrace();
        }
        initWindow();
        initOther();
        initTrayIcon();
    }
    
private   void  initOther(){
        jtp
= new  JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
    }
    
private   void  initWindow(){
        start
= new  JButton( " 开始截取 " );
        saveAll
= new  JButton( " 保存所有 " );
        cancel
= new  JButton( " 退出 " );
        start.addActionListener(
this );
        saveAll.addActionListener(
this );
        cancel.addActionListener(
this );
        JPanel buttonJP
= new  JPanel();
        c
= new  JPanel( new  BorderLayout());
        c.setBackground(Color.BLACK);
        JLabel jl
= new  JLabel( " 屏幕截取 " ,JLabel.CENTER);
        JLabel jl1
= new  JLabel( " <Html><Font size=5 color=white>作者:千里冰封<br> "   +
                
" QQ:24325142<br><br><br></Font></html> " ,JLabel.CENTER);
        jl.setFont(
new  Font( " 黑体 " ,Font.BOLD, 40 ));
        jl.setForeground(Color.RED);
        jl1.setForeground(Color.BLUE);
        c.add(jl,BorderLayout.CENTER);
        c.add(jl1,BorderLayout.SOUTH);
        buttonJP.add(start);
        buttonJP.add(saveAll);
        buttonJP.add(cancel);
        buttonJP.setBorder(BorderFactory.createTitledBorder(
" 公共操作区 " ));
        JPanel jp
= new  JPanel(); // 放两个单选按钮的面板
        jp.add(java = new  JRadioButton( " java界面 " ));
        jp.add(system
= new  JRadioButton( " 系统界面 " , true ));
        java.addActionListener(
this );
        system.addActionListener(
this );
        jp.setBorder(BorderFactory.createTitledBorder(
" 界面风格 " ));
        ButtonGroup bg
= new  ButtonGroup();
        bg.add(java);
        bg.add(system);
        JPanel all
= new  JPanel();
        all.add(jp);
        all.add(buttonJP);
        
this .getContentPane().add(c,BorderLayout.CENTER);
        
this .getContentPane().add(all,BorderLayout.SOUTH);
        
this .setSize( 500 , 400 );
        
this .setLocationRelativeTo( null );
        
this .setVisible( true );
        
this .setAlwaysOnTop( true );
        
this .setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        
this .addWindowListener( new  WindowAdapter(){
            
public   void  windowClosing(WindowEvent we){
                CaptureScreen.
this .setVisible( false );
            }
        });
    }
    
private   void  initTrayIcon(){
        
try {
            SystemTray st
= SystemTray.getSystemTray();
            Image im
= ImageIO.read( this .getClass().getResource( " bg.gif " ));
            PopupMenu pm
= new  PopupMenu( " 弹出菜单 " );
            pm.add(
new  MenuItem( " 关于 " )).addActionListener( new  ActionListener(){
                
public   void  actionPerformed(ActionEvent ae){
                    JOptionPane.showMessageDialog(CaptureScreen.
this , " <html><Font color=red><center><h2>关于</h2></center></Font> "   +
                            
" 这是一款纯JAVA的屏幕截取程序<br>在以前的基础上增加了一些常用的功能,<br> "   +
                            
" 比如,批量保存,多幅截取,复制到系统粘帖板<br> "   +
                            
" 在使用过程中有任何问题,欢迎联系.<br> "   +
                            
" <Font size=5 color=blue>作者:千里冰封<br> "   +
                            
" <i>QQ:24325142</i><br></Font></html> " );
                }
            });
            pm.addSeparator();
            pm.add(
new  MenuItem( " 显示主窗口 " )).addActionListener( new  ActionListener(){
                
public   void  actionPerformed(ActionEvent ae){
                    CaptureScreen.
this .setVisible( true );
                }
            });
            pm.add(
new  MenuItem( " 开始截取 " )).addActionListener( new  ActionListener(){
                
public   void  actionPerformed(ActionEvent ae){
                    doStart();
                }
            });
            pm.add(
new  MenuItem( " 退出程序 " )).addActionListener( new  ActionListener(){
                
public   void  actionPerformed(ActionEvent ae){
                    System.exit(
0 );
                }
            });
            TrayIcon ti
= new  TrayIcon(im, " JAVA屏幕截取 " ,pm);
            st.add(ti);
            ti.addActionListener(
new  ActionListener(){
                
public   void  actionPerformed(ActionEvent ae){
                    CaptureScreen.
this .setVisible( true );
                }
            });
        }
catch (Exception exe){
            exe.printStackTrace();
        }
    }
    
private   void  updates(){
        
this .setVisible( true );
        
if (get != null ){
            
// 如果索引是0,则表示一张图片都没有被加入过,
            
// 则要清除当前的东西,重新把tabpane放进来
             if (index == 0 ){
                c.removeAll();
                c.add(jtp,BorderLayout.CENTER);
            }
else { // 否则的话,直接对tabpane添加面板就可以了
                
// 就什么都不用做了
            }
            PicPanel pic
= new  PicPanel(get);
            jtp.addTab(
" 图片 " + ( ++ index),pic);
            jtp.setSelectedComponent(pic);
            SwingUtilities.updateComponentTreeUI(c);
        }
    }
    
private   void  doStart(){
        
try {
            
this .setVisible( false );
            Thread.sleep(
500 ); // 睡500毫秒是为了让主窗完全不见
            Robot ro = new  Robot();
            Toolkit tk
= Toolkit.getDefaultToolkit();
            Dimension di
= tk.getScreenSize();
            Rectangle rec
= new  Rectangle( 0 , 0 ,di.width,di.height);
            BufferedImage bi
= ro.createScreenCapture(rec);
            JFrame jf
= new  JFrame();
            Temp temp
= new  Temp(jf,bi,di.width,di.height);
            jf.getContentPane().add(temp,BorderLayout.CENTER);
            jf.setUndecorated(
true );
            jf.setSize(di);
            jf.setVisible(
true );
            jf.setAlwaysOnTop(
true );
        } 
catch (Exception exe){
            exe.printStackTrace();
        }
    }
    
/**
     *公共方法,处理保存所有的图片
     
*/
    
public   void  doSaveAll(){
        
if (jtp.getTabCount() == 0 ){
            JOptionPane.showMessageDialog(
this , " 图片不能为空!! " , " 错误 " ,JOptionPane.ERROR_MESSAGE);
            
return ;
        }
        JFileChooser jfc
= new  JFileChooser( " . " );
        jfc.addChoosableFileFilter(
new  GIFfilter());
        jfc.addChoosableFileFilter(
new  BMPfilter());
        jfc.addChoosableFileFilter(
new  JPGfilter());
        jfc.addChoosableFileFilter(
new  PNGfilter());
        
int  i = jfc.showSaveDialog( this );
        
if (i == JFileChooser.APPROVE_OPTION){
            File file
= jfc.getSelectedFile();
            String about
= " PNG " ;
            String ext
= file.toString().toLowerCase();
            javax.swing.filechooser.FileFilter ff
= jfc.getFileFilter();
            
if (ff  instanceof  JPGfilter){
                about
= " JPG " ;
            } 
else   if (ff  instanceof  PNGfilter){
                about
= " PNG " ;
            }
else   if (ff  instanceof  BMPfilter){
                about
= " BMP " ;
            }
else   if (ff  instanceof  GIFfilter){
                about
= " GIF " ;
            }
            
if (ext.endsWith(about.toLowerCase())){
                ext
= ext.substring( 0 ,ext.lastIndexOf(about.toLowerCase()));
            }
            
// 起一个线程去保存这些图片并显示出进度条
             new  SaveAllThread(ext,about).setVisible( true );
        }
        
    }
    
// 专门用来保存所有图片的线程类,它还要显示出保存的进度条
     private   class  SaveAllThread  extends  JDialog  implements  Runnable{
        
private  String name; // 文件名头部份
         private  String ext; // 文件格式
         private  JProgressBar jpb; // 一个进度条
         private  JLabel info; //  一个信息显示条
         private   int  allTask,doneTask; // 所有任务,已完成任务
         public  SaveAllThread(String name,String ext){
            
super (CaptureScreen. this , " 保存 " , true );
            
this .name = name;
            
this .ext = ext;
            initWindow();
        }
        
private   void  initWindow(){
            jpb
= new  JProgressBar();
            allTask
= jtp.getTabCount();
            jpb.setMaximum(allTask);
            jpb.setMinimum(
0 );
            jpb.setValue(
0 );
            jpb.setStringPainted(
true );
            setProgressBarString();
            info
= new  JLabel( " 正在保存到: " );
            
this .getContentPane().setBackground(Color.CYAN);
            
this .add(info,BorderLayout.NORTH);
            
this .add(jpb,BorderLayout.SOUTH);
            
this .setUndecorated( true );
            
this .setSize( 300 , 100 );
            
this .setLocationRelativeTo(CaptureScreen. this );
            
new  Thread( this ).start();
        }
        
private   void  setProgressBarString(){
            jpb.setString(
"" + doneTask + " / " + allTask);
        }
        
public   void  run(){
            
try {
                
for ( int  i = 0 ;i < allTask;i ++ ){
                    PicPanel pp
= (PicPanel)jtp.getComponentAt(i);
                    BufferedImage image
= pp.getImage();
                    File f
=   new  File(name + (doneTask + 1 ) + " . " + ext.toLowerCase());
                    info.setText(
" <html><b>正在保存到:</b><br> " + f.toString() + " </html> " );
                    ImageIO.write(image,ext,f);
                    doneTask
++ ;
                    jpb.setValue(doneTask);
                    setProgressBarString();
                    Thread.sleep(
500 );
                }
                JOptionPane.showMessageDialog(
this , " 保存完毕!! " );
                
this .dispose();
            }
catch (Exception exe){
                exe.printStackTrace();
                
this .dispose();
            }
        }
    }
    
/**
     *公用的处理保存图片的方法
     *这个方法不再私有了
     
*/
    
public    void  doSave(BufferedImage get){
        
try {
            
if (get == null ){
                JOptionPane.showMessageDialog(
this , " 图片不能为空!! " , " 错误 " ,JOptionPane.ERROR_MESSAGE);
                
return ;
            }
            JFileChooser jfc
= new  JFileChooser( " . " );
            jfc.addChoosableFileFilter(
new  GIFfilter());
            jfc.addChoosableFileFilter(
new  BMPfilter());
            jfc.addChoosableFileFilter(
new  JPGfilter());
            jfc.addChoosableFileFilter(
new  PNGfilter());
            
int  i = jfc.showSaveDialog( this );
            
if (i == JFileChooser.APPROVE_OPTION){
                File file
= jfc.getSelectedFile();
                String about
= " PNG " ;
                String ext
= file.toString().toLowerCase();
                javax.swing.filechooser.FileFilter ff
= jfc.getFileFilter();
                
if (ff  instanceof  JPGfilter){
                    about
= " JPG " ;
                    
if ( ! ext.endsWith( " .jpg " )){
                        String ns
= ext + " .jpg " ;
                        file
= new  File(ns);
                    }
                } 
else   if (ff  instanceof  PNGfilter){
                    about
= " PNG " ;
                    
if ( ! ext.endsWith( " .png " )){
                        String ns
= ext + " .png " ;
                        file
= new  File(ns);
                        
                    }
                }
else   if (ff  instanceof  BMPfilter){
                    about
= " BMP " ;
                    
if ( ! ext.endsWith( " .bmp " )){
                        String ns
= ext + " .bmp " ;
                        file
= new  File(ns);
                        
                &n

你可能感兴趣的:(java,swing,qq,C#,ext)