<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    千里冰封
    JAVA 濃香四溢
    posts - 151,comments - 2801,trackbacks - 0
    有些時候截屏是很有必要的,可是不可能每次都開著QQ在那里截吧,也不可能按print sreen鍵,再后把整個桌面都截下來吧,這個時候,有一個自己的截屏程序是很有必要的,并且可以自己截成任意大小,任意位置.用法和當時QQ的截屏差不多.可以選區拖動,縮放選區,雙擊保存,右鍵選區是取消選區,右鍵別的地方是退出截屏程序.
    /*
     * CaptureScreen.java
     *
     * Created on 2006年9月7日, 上午10:59
     *
     * To change this template, choose Tools | Template Manager
     * and open the template in the editor.
     
    */

    package test1;

    /**
     *
     * 
    @author lbf
     
    */
    import java.awt.*;
    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,save;
        
    private JPanel c;
        
    private BufferedImage get;
        
    /** Creates a new instance of CaptureScreen */
        
    public CaptureScreen() {
            
    super("屏幕截取軟件");
            initWindow();
        }
        
    private void initWindow(){
            start
    =new JButton("開始截取");
            cancel
    =new JButton("退出");
            save
    =new JButton("保存");
            save.setEnabled(
    false);
            save.addActionListener(
    this);
            start.addActionListener(
    this);
            cancel.addActionListener(
    this);
            JPanel buttonJP
    =new JPanel();
            c
    =new JPanel(new BorderLayout());
            JLabel jl
    =new JLabel("屏幕截取",JLabel.CENTER);
            JLabel jl1
    =new JLabel("作者:千里冰封",JLabel.CENTER);
            jl.setFont(
    new Font("黑體",Font.BOLD,40));
            jl1.setFont(
    new Font("宋體",Font.BOLD,20));
            jl.setForeground(Color.RED);
            jl1.setForeground(Color.BLUE);
            c.add(jl,BorderLayout.CENTER);
            c.add(jl1,BorderLayout.SOUTH);
            buttonJP.add(start);
            buttonJP.add(save);
            buttonJP.add(cancel);
            
    this.getContentPane().add(c,BorderLayout.CENTER);
            
    this.getContentPane().add(buttonJP,BorderLayout.SOUTH);
            
    this.setSize(300,300);
            
    this.setLocationRelativeTo(null);
            
    this.setVisible(true);
            
    this.setAlwaysOnTop(true);
            
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
    private void updates(){
            
    if(get!=null){
                ImageIcon ii
    =new ImageIcon(get);
                JLabel jl
    =new JLabel(ii);
                c.removeAll();
                c.add(
    new JScrollPane(jl),BorderLayout.CENTER);
                SwingUtilities.updateComponentTreeUI(
    this);
            }
        }
        
    private void doStart(){
            
    try{
                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();
                jf.getContentPane().add(
    new Temp(jf,bi,di.width,di.height));
                jf.setUndecorated(
    true);
                jf.setSize(di);
                jf.setVisible(
    true);
                jf.setAlwaysOnTop(
    true);
            } 
    catch(Exception exe){
                exe.printStackTrace();
            }
        }
        
    private void doSave(){
            
    try{
                JFileChooser jfc
    =new JFileChooser(".");
                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){
                        
    if(!ext.endsWith(".jpg")){
                            String ns
    =ext+".jpg";
                            file
    =new File(ns);
                            about
    ="JPG";
                        }
                    } 
    else if(ff instanceof PNGfilter){
                        
    if(!ext.endsWith(".png")){
                            String ns
    =ext+".png";
                            file
    =new File(ns);
                            about
    ="PNG";
                        }
                    }
                    
                    
    if(ImageIO.write(get,about,file)){
                        JOptionPane.showMessageDialog(
    this,"保存成功!");
                        save.setEnabled(
    false);
                    } 
    else
                        JOptionPane.showMessageDialog(
    this,"保存失敗!");
                }
            } 
    catch(Exception exe){
                exe.printStackTrace();
            }
        }
        
    public void actionPerformed(ActionEvent ae){
            
    if(ae.getSource()==start){
                doStart();
            } 
    else if(ae.getSource()==cancel){
                System.exit(
    0);
            } 
    else if(ae.getSource()==save){
                doSave();
            }
        }
        
    //一個文件后綴名選擇器
        private class JPGfilter extends javax.swing.filechooser.FileFilter{
            
    public JPGfilter(){
                
            }
            
    public boolean accept(File file){
                
    if(file.toString().toLowerCase().endsWith(".jpg")||
                        file.toString().toLowerCase().endsWith(
    ".jpeg")||
                        file.isDirectory()){
                    
    return true;
                } 
    else
                    
    return false;
            }
            
    public String getDescription(){
                
    return "*.JPG,*.JPEG(JPG,JPEG圖像)";
            }
        }
        
    private class PNGfilter extends javax.swing.filechooser.FileFilter{
            
    public boolean accept(File file){
                
    if(file.toString().toLowerCase().endsWith(".png")||
                        file.isDirectory()){
                    
    return true;
                } 
    else
                    
    return false;
            }
            
    public String getDescription(){
                
    return "*.PNG(PNG圖像)";
            }
        }
        
    //一個暫時類,用于顯示當前的屏幕圖像
        private class Temp extends JPanel implements MouseListener,MouseMotionListener{
            
    private BufferedImage bi;
            
    private int width,height;
            
    private int startX,startY,endX,endY,tempX,tempY;
            
    private JFrame jf;
            
    private Rectangle select=new Rectangle(0,0,0,0);//表示選中的區域
            private Cursor cs;//表示一般情況下的鼠標狀態
            private States current=States.DEFAULT;// 表示當前的編輯狀態
            private Rectangle[] rec;//表示八個編輯點的區域
            public Temp(JFrame jf,BufferedImage bi,int width,int height){
                
    this.jf=jf;
                
    this.bi=bi;
                
    this.width=width;
                
    this.height=height;
                
    this.addMouseListener(this);
                
    this.addMouseMotionListener(this);
                Image icon
    =Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("icon.png"));
                cs
    =Toolkit.getDefaultToolkit().createCustomCursor(icon,new Point(0,0),"icon");
                
    this.setCursor(cs);
                initRecs();
            }
            
    private void initRecs(){
                rec
    =new Rectangle[8];
                
    for(int i=0;i<rec.length;i++){
                    rec[i]
    =new Rectangle();
                }
            }
            
    public void paintComponent(Graphics g){
                g.drawImage(bi,
    0,0,width,height,this);
                g.setColor(Color.RED);
                g.drawLine(startX,startY,endX,startY);
                g.drawLine(startX,endY,endX,endY);
                g.drawLine(startX,startY,startX,endY);
                g.drawLine(endX,startY,endX,endY);
                
    int x=startX<endX?startX:endX;
                
    int y=startY<endY?startY:endY;
                select
    =new Rectangle(x,y,Math.abs(endX-startX),Math.abs(endY-startY));
                
    int x1=(startX+endX)/2;
                
    int y1=(startY+endY)/2;
                g.fillRect(x1
    -2,startY-2,5,5);
                g.fillRect(x1
    -2,endY-2,5,5);
                g.fillRect(startX
    -2,y1-2,5,5);
                g.fillRect(endX
    -2,y1-2,5,5);
                g.fillRect(startX
    -2,startY-2,5,5);
                g.fillRect(startX
    -2,endY-2,5,5);
                g.fillRect(endX
    -2,startY-2,5,5);
                g.fillRect(endX
    -2,endY-2,5,5);
                rec[
    0]=new Rectangle(x-5,y-5,10,10);
                rec[
    1]=new Rectangle(x1-5,y-5,10,10);
                rec[
    2]=new Rectangle((startX>endX?startX:endX)-5,y-5,10,10);
                rec[
    3]=new Rectangle((startX>endX?startX:endX)-5,y1-5,10,10);
                rec[
    4]=new Rectangle((startX>endX?startX:endX)-5,(startY>endY?startY:endY)-5,10,10);
                rec[
    5]=new Rectangle(x1-5,(startY>endY?startY:endY)-5,10,10);
                rec[
    6]=new Rectangle(x-5,(startY>endY?startY:endY)-5,10,10);
                rec[
    7]=new Rectangle(x-5,y1-5,10,10);
            }
            
    public void mouseMoved(MouseEvent me){
                
    if(select.contains(me.getPoint())){
                    
    this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
                    current
    =States.MOVE;
                } 
    else{
                    States[] st
    =States.values();
                    
    for(int i=0;i<rec.length;i++){
                        
    if(rec[i].contains(me.getPoint())){
                            current
    =st[i];
                            
    this.setCursor(st[i].getCursor());
                            
    return;
                        }
                    }
                    
    this.setCursor(cs);
                    current
    =States.DEFAULT;
                }
            }
            
    public void mouseExited(MouseEvent me){
                
            }
            
    public void mouseEntered(MouseEvent me){
                
            }
            
    public void mouseDragged(MouseEvent me){
                
    int x=me.getX();
                
    int y=me.getY();
                
    if(current==States.MOVE){
                    startX
    +=(x-tempX);
                    startY
    +=(y-tempY);
                    endX
    +=(x-tempX);
                    endY
    +=(y-tempY);
                    tempX
    =x;
                    tempY
    =y;
                }
    else if(current==States.EAST){
                    
    if(startX>endX){
                        startX
    +=(x-tempX);
                        tempX
    =x;
                    } 
    else{
                        endX
    +=(x-tempX);
                        tempX
    =x;
                    }
                }
    else if(current==States.NORTH){
                    
    if(startY<endY){
                        startY
    +=(y-tempY);
                        tempY
    =y;
                    }
    else{
                        endY
    +=(y-tempY);
                        tempY
    =y;
                    }
                }
    else if(current==States.WEST){
                    
    if(startX<endX){
                        startX
    +=(x-tempX);
                        tempX
    =x;
                    } 
    else{
                        endX
    +=(x-tempX);
                        tempX
    =x;
                    }
                }
    else if(current==States.SOUTH){
                    
    if(startY>endY){
                        startY
    +=(y-tempY);
                        tempY
    =y;
                    }
    else{
                        endY
    +=(y-tempY);
                        tempY
    =y;
                    }
                } 
    else if(current==States.NORTH_EAST){
                    
    if(startX>endX){
                        startX
    +=(x-tempX);
                        tempX
    =x;
                    } 
    else{
                        endX
    +=(x-tempX);
                        tempX
    =x;
                    }
                    
    if(startY<endY){
                        startY
    +=(y-tempY);
                        tempY
    =y;
                    }
    else{
                        endY
    +=(y-tempY);
                        tempY
    =y;
                    }
                }
    else if(current==States.NORTH_WEST){
                    
    if(startX<endX){
                        startX
    +=(x-tempX);
                        tempX
    =x;
                    } 
    else{
                        endX
    +=(x-tempX);
                        tempX
    =x;
                    }
                    
    if(startY<endY){
                        startY
    +=(y-tempY);
                        tempY
    =y;
                    }
    else{
                        endY
    +=(y-tempY);
                        tempY
    =y;
                    }
                }
    else if(current==States.SOUTH_EAST){
                    
    if(startY>endY){
                        startY
    +=(y-tempY);
                        tempY
    =y;
                    }
    else{
                        endY
    +=(y-tempY);
                        tempY
    =y;
                    }
                    
    if(startX>endX){
                        startX
    +=(x-tempX);
                        tempX
    =x;
                    } 
    else{
                        endX
    +=(x-tempX);
                        tempX
    =x;
                    }
                }
    else if(current==States.SOUTH_WEST){
                    
    if(startY>endY){
                        startY
    +=(y-tempY);
                        tempY
    =y;
                    }
    else{
                        endY
    +=(y-tempY);
                        tempY
    =y;
                    }
                    
    if(startX<endX){
                        startX
    +=(x-tempX);
                        tempX
    =x;
                    } 
    else{
                        endX
    +=(x-tempX);
                        tempX
    =x;
                    }
                }

                
    else{
                    startX
    =tempX;
                    startY
    =tempY;
                    endX
    =me.getX();
                    endY
    =me.getY();
                }
                
    this.repaint();
            }
            
    public void mousePressed(MouseEvent me){
                tempX
    =me.getX();
                tempY
    =me.getY();
            }
            
    public void mouseReleased(MouseEvent me){
                
    if(me.isPopupTrigger()){
                    
    if(current==States.MOVE){
                        startX
    =0;
                        startY
    =0;
                        endX
    =0;
                        endY
    =0;
                        repaint();
                    } 
    else{
                        jf.dispose();
                        updates();
                    }
                    
                }
            }
            
    public void mouseClicked(MouseEvent me){
                
    if(me.getClickCount()==2){
                    
    //Rectangle rec=new Rectangle(startX,startY,Math.abs(endX-startX),Math.abs(endY-startY));
                    Point p=me.getPoint();
                    
    if(select.contains(p)){
                        
    if(select.x+select.width<this.getWidth()&&select.y+select.height<this.getHeight()){
                            get
    =bi.getSubimage(select.x,select.y,select.width,select.height);
                            jf.dispose();
                            save.setEnabled(
    true);
                            updates();
                        }
    else{
                            
    int wid=select.width,het=select.height;
                            
    if(select.x+select.width>=this.getWidth()){
                                wid
    =this.getWidth()-select.x;
                            }
                            
    if(select.y+select.height>=this.getHeight()){
                                het
    =this.getHeight()-select.y;
                            }
                            get
    =bi.getSubimage(select.x,select.y,wid,het);
                            jf.dispose();
                            save.setEnabled(
    true);
                            updates();
                        }
                        
                    }
                }
            }
        }
        
        
    public static void main(String args[]){
            
    new CaptureScreen();
        }
    }
    enum States{
        NORTH_WEST(
    new Cursor(Cursor.NW_RESIZE_CURSOR)),//表示西北角
        NORTH(new Cursor(Cursor.N_RESIZE_CURSOR)),
        NORTH_EAST(
    new Cursor(Cursor.NE_RESIZE_CURSOR)),
        EAST(
    new Cursor(Cursor.E_RESIZE_CURSOR)),
        SOUTH_EAST(
    new Cursor(Cursor.SE_RESIZE_CURSOR)),
        SOUTH(
    new Cursor(Cursor.S_RESIZE_CURSOR)),
        SOUTH_WEST(
    new Cursor(Cursor.SW_RESIZE_CURSOR)),
        WEST(
    new Cursor(Cursor.W_RESIZE_CURSOR)),
        MOVE(
    new Cursor(Cursor.MOVE_CURSOR)),
        DEFAULT(
    new Cursor(Cursor.DEFAULT_CURSOR));
        
    private Cursor cs;
        States(Cursor cs){
            
    this.cs=cs;
        }
        
    public Cursor getCursor(){
            
    return cs;
        }
    }





    盡管千里冰封
    依然擁有晴空

    你我共同品味JAVA的濃香.
    posted on 2007-08-30 10:31 千里冰封 閱讀(2045) 評論(12)  編輯  收藏 所屬分類: JAVASE

    FeedBack:
    # re: JAVA截屏程序
    2007-08-30 11:30 | JAVA面試題
    給要面試的朋友推薦一個java面試題庫:
    http://www.teecool.com/catalog.asp?tags=JAVA%E9%9D%A2%E8%AF%95%E9%A2%98  回復  更多評論
      
    # re: JAVA截屏程序
    2007-08-30 12:45 | BeanSoft
    寫的非常好, 感謝分享! 收藏, 以后整合進鄙人的 Code Manager .SWT 不介意吧?  回復  更多評論
      
    # re: JAVA截屏程序
    2007-08-30 12:45 | BeanSoft
    小建議: 開始選屏后隱藏主窗口會更好...  回復  更多評論
      
    # re: JAVA截屏程序
    2007-08-30 12:48 | 千里冰封
    @BeanSoft
    當然不介意,呵呵,大家互相交流嘛  回復  更多評論
      
    # re: JAVA截屏程序
    2007-08-30 16:07 | kafei
    多謝了,拿去研究一下  回復  更多評論
      
    # re: JAVA截屏程序
    2007-08-30 16:14 | zhongyu
    收了,學習一下。  回復  更多評論
      
    # re: JAVA截屏程序
    2007-08-30 16:40 | 久城
    不會,學習下。  回復  更多評論
      
    # re: JAVA截屏程序
    2007-09-20 21:15 | wangts
    不錯,學習學習。  回復  更多評論
      
    # re: JAVA截屏程序
    2007-12-17 21:27 | 日月雨林
    為什么我的會出現下面的錯誤呢???

    D:\JavaTest>java CaptureScreen
    Uncaught error fetching image:
    java.lang.NullPointerException
    at sun.awt.image.URLImageSource.getConnection(Unknown Source)
    at sun.awt.image.URLImageSource.getDecoder(Unknown Source)
    at sun.awt.image.InputStreamImageSource.doFetch(Unknown Source)
    at sun.awt.image.ImageFetcher.fetchloop(Unknown Source)
    at sun.awt.image.ImageFetcher.run(Unknown Source)
      回復  更多評論
      
    # re: JAVA截屏程序
    2008-04-16 10:49 | YYMMIINNGG
    @日月雨林
    這是因為你沒有放置bg.jif圖片作為系統托盤圖標  回復  更多評論
      
    # re: JAVA截屏程序[未登錄]
    2010-01-12 12:47 |
    要怎么使用??? 我要嵌套到網頁里的? 感謝?。?!  回復  更多評論
      
    # re: JAVA截屏程序[未登錄]
    2011-05-26 15:43 | peter
    你好,請問:這個截屏程序放在服務器上,客戶端訪問的時候,觸發截屏事件,沒有響應,服務器卻處在了截屏狀態。這個跟JAVA機制有關,是吧?怎么解決這個問題呢?怎么放到前臺執行呢? 希望與你討論:QQ200803162  回復  更多評論
      
    主站蜘蛛池模板: 亚洲精品91在线| 无码av免费一区二区三区试看| 久久亚洲国产成人亚| 国产精品深夜福利免费观看| 84pao强力永久免费高清| aaa毛片免费观看| 美女黄色毛片免费看| 亚洲真人无码永久在线观看| 亚洲高清在线mv| 亚洲VA中文字幕不卡无码| www.91亚洲| 四虎永久在线精品免费影视| 性xxxx视频播放免费| 2021久久精品免费观看| 久久国产精品成人片免费| 久久久久成人片免费观看蜜芽 | a级毛片免费观看视频| 免费无码国产V片在线观看| 亚洲精品国产综合久久久久紧 | 蜜桃AV无码免费看永久| 久久国产免费一区| 国产日韩AV免费无码一区二区 | 日本大片在线看黄a∨免费| 噼里啪啦电影在线观看免费高清| 毛片免费全部播放无码| 最近免费中文字幕大全免费版视频 | 色天使亚洲综合一区二区| 亚洲色成人WWW永久在线观看| 亚洲沟沟美女亚洲沟沟| 亚洲精品福利网泷泽萝拉| 久久久久亚洲AV无码麻豆| 亚洲自偷自拍另类12p| 久久精品国产亚洲AV无码麻豆| 亚洲国产精品lv| 亚洲福利电影在线观看| 亚洲国产片在线观看| 国产精品亚洲一区二区麻豆| 亚洲综合一区国产精品| 亚洲国产欧美一区二区三区| 日韩亚洲综合精品国产| 国产成人不卡亚洲精品91|