千里冰封
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 千里冰封 阅读(2024) 评论(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  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: