千里冰封
          JAVA 濃香四溢
          posts - 151,comments - 2801,trackbacks - 0
          有些時(shí)候截屏是很有必要的,可是不可能每次都開著QQ在那里截吧,也不可能按print sreen鍵,再后把整個(gè)桌面都截下來吧,這個(gè)時(shí)候,有一個(gè)自己的截屏程序是很有必要的,并且可以自己截成任意大小,任意位置.用法和當(dāng)時(shí)QQ的截屏差不多.可以選區(qū)拖動(dòng),縮放選區(qū),雙擊保存,右鍵選區(qū)是取消選區(qū),右鍵別的地方是退出截屏程序.
          /*
           * 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();
                  }
              }
              
          //一個(gè)文件后綴名選擇器
              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圖像)";
                  }
              }
              
          //一個(gè)暫時(shí)類,用于顯示當(dāng)前的屏幕圖像
              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);//表示選中的區(qū)域
                  private Cursor cs;//表示一般情況下的鼠標(biāo)狀態(tài)
                  private States current=States.DEFAULT;// 表示當(dāng)前的編輯狀態(tài)
                  private Rectangle[] rec;//表示八個(gè)編輯點(diǎn)的區(qū)域
                  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 千里冰封 閱讀(2041) 評(píng)論(12)  編輯  收藏 所屬分類: JAVASE

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

          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)
            回復(fù)  更多評(píng)論
            
          # re: JAVA截屏程序
          2008-04-16 10:49 | YYMMIINNGG
          @日月雨林
          這是因?yàn)槟銢]有放置bg.jif圖片作為系統(tǒng)托盤圖標(biāo)  回復(fù)  更多評(píng)論
            
          # re: JAVA截屏程序[未登錄]
          2010-01-12 12:47 |
          要怎么使用啊? 我要嵌套到網(wǎng)頁里的? 感謝?。?!  回復(fù)  更多評(píng)論
            
          # re: JAVA截屏程序[未登錄]
          2011-05-26 15:43 | peter
          你好,請(qǐng)問:這個(gè)截屏程序放在服務(wù)器上,客戶端訪問的時(shí)候,觸發(fā)截屏事件,沒有響應(yīng),服務(wù)器卻處在了截屏狀態(tài)。這個(gè)跟JAVA機(jī)制有關(guān),是吧?怎么解決這個(gè)問題呢?怎么放到前臺(tái)執(zhí)行呢? 希望與你討論:QQ200803162  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 奉贤区| 红原县| 施秉县| 正阳县| 山东省| 新宁县| 如东县| 定西市| 嘉善县| 清镇市| 浪卡子县| 揭阳市| 延安市| 海阳市| 武威市| 花莲市| 徐汇区| 黄浦区| 同仁县| 克东县| 永福县| 长垣县| 思南县| 余姚市| 北辰区| 田林县| 扬州市| 亚东县| 泌阳县| 新宁县| 东阳市| 溧水县| 大埔县| 崇文区| 西乌珠穆沁旗| 平度市| 石家庄市| 商河县| 澄城县| 临沭县| 西乌珠穆沁旗|