闲着无聊用java做了个截图的小工具.......
由于不知道怎么在程序界面之外来启动监听(有谁知道吗?在JFrame之外监听鼠标事件??) 就用了其他的方式完成(一块半透明的JFrame..........)
实现做的不是很好 不能拖动截图 是单击三次截图(第一次确定截图的左上角坐标 第二次选择大小 第三次截图)
打开文件夹方面直接用了explorer 所以....光是这点就没有了可移植性....
主要用到的是Robot和AWTUtilies这两个类
package Work;import java.awt.Font;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JRootPane;
import javax.swing.JTextArea;import com.sun.awt.AWTUtilities;public class SnapClient implements ActionListener,ComponentListener{
JFrame info=null;
SnapRobot sr=null;
JFrame jf=null;
JButton snapAll=null;
JButton snapPart=null;
JButton setting=null;
JButton open=null;
JButton about=null;
private String directoryName="D:";
private boolean showInfo=true;
public SnapClient(){
sr=new SnapRobot();
jf=new JFrame("   SnapTool V1.0");
jf.setSize(100, 160);
jf.setLayout(new GridLayout(5,1,5,5));
jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
snapAll=new JButton("截取全屏幕");
snapPart=new JButton("截取屏幕");
setting=new JButton("设置目录");
open=new JButton("打开目录");
about=new JButton("关于");
snapAll.setContentAreaFilled(false);
snapPart.setContentAreaFilled(false);
setting.setContentAreaFilled(false);
open.setContentAreaFilled(false);
about.setContentAreaFilled(false);
jf.add(snapAll);
jf.add(snapPart);
jf.add(setting);
jf.add(open);
jf.add(about);
jf.setResizable(false);
jf.setVisible(true);
jf.addComponentListener(this);
AWTUtilities.setWindowOpacity(jf,(float) 0.9);
snapAll.addActionListener(this);
snapPart.addActionListener(this);
setting.addActionListener(this);
open.addActionListener(this);
about.addActionListener(this);
//info 关于信息:
info=new JFrame("关于");
JTextArea jta=new JTextArea();
jta.append("\ncode by Fair_jm \n");
jta.append("透明效果实现:\nAWTUtilities\n");
jta.append("截图实现:Robot\n");
jta.append("联系方式:\[email protected]\n");
jta.append("date:2012-2-25\n");
info.add(jta);
info.setUndecorated(true);
info.setSize(160,160);
info.setLocation(jf.getX()+110, jf.getY());
AWTUtilities.setWindowOpacity(info,(float) 0.3);
info.setVisible(!showInfo);
jta.setEditable(false);

}

//按钮判定
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==snapAll){
snapAll();

}
if(e.getSource()==snapPart){
           snapPart();
}
if(e.getSource()==setting){
            doSetting();
}
if(e.getSource()==open){
             doOpen();
}
if(e.getSource()==about){
showAbout();
}

}

public void showAbout(){
info.setVisible(showInfo);
showInfo=!showInfo;
}

//打开文件夹 用了exec调用本地程序explorer
public void doOpen(){
try {
Runtime.getRuntime().exec("explorer "+this.directoryName);
} catch (IOException e1) {
e1.printStackTrace();
}
}

//进行文件夹的选择
public void doSetting(){
JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int flag=chooser.showOpenDialog(null);
        if(flag==JFileChooser.APPROVE_OPTION){
         this.directoryName=chooser.getSelectedFile().getPath();
        }
        System.out.println(this.directoryName);
        sr.setdirectoryName(this.directoryName);
}

//截取部分图片 此功能需要优化 截图画面太不美观了 %>_<%
public void snapPart(){
jf.setVisible(false);
info.setVisible(false);
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
new SnapWindow(this.sr);
jf.setVisible(true);
info.setVisible(!showInfo);
}

//截取全屏  
public void snapAll(){
jf.setVisible(false);
info.setVisible(false);
try {
Thread.sleep(50);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
sr.draw();
info.setVisible(!showInfo);
jf.setVisible(true);
}

public static void main(String[] args) {
new SnapClient();

@Override
public void componentHidden(ComponentEvent arg0) {
info.setVisible(false);
}
@Override
public void componentMoved(ComponentEvent arg0) {
if(info!=null){
info.setLocation(jf.getLocationOnScreen().x+110, jf.getLocationOnScreen().y);
if(!showInfo){
            info.setVisible(false);
info.setVisible(true);
}
}

} @Override
public void componentResized(ComponentEvent arg0) {
}
@Override
public void componentShown(ComponentEvent arg0) {
info.setVisible(!showInfo);

}}
//code by cc Fair_jm 欢迎修改及使用^_^ 20120225

解决方案 »

  1.   

    package Work;import java.awt.AWTException;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.Toolkit;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;import javax.imageio.ImageIO;public class SnapRobot {
      Robot r;
      String directoryName=null;
      Rectangle rg=null;public SnapRobot(){
    try {
    r=new Robot();
    } catch (AWTException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    public String getdirectoryName() {
    return directoryName;
    }
    public void setdirectoryName(String directoryName) {
    this.directoryName = directoryName;
    }public Rectangle getRg() {
    return rg;
    }
    public void setRg(Rectangle rg) {
    this.rg = rg;
    }
    public void draw(){
    if (directoryName==null){
    directoryName="D:";
    }
    if(rg==null){
    rg=new Rectangle(0,0,(int)Toolkit.getDefaultToolkit().getScreenSize().getWidth(),
    (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight());
    }
    BufferedImage bi=r.createScreenCapture(rg);
        try {
    ImageIO.write(bi, "png",new File(this.directoryName+File.separator+this.getFileName()) );
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }public String getFileName(){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSS");
    return sdf.format(new Date())+".png";
    }
    }
    //code by cc Fair_jm 欢迎修改及使用^_^ 20120125
    package Work;import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    import java.awt.Toolkit;
    import java.awt.Window;
    import java.awt.event.ComponentEvent;
    import java.awt.event.ComponentListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;import com.sun.awt.AWTUtilities;import javax.swing.JFrame;
    import javax.swing.JRootPane;class SnapWindow extends JFrame implements ComponentListener,MouseListener,MouseMotionListener
    {
    private int x;
    private int y;
    private int width;
    private int height;
    private final int MAXX;
    private final int MAXY;

        private boolean firstClick=false;
        private boolean secondClick=false;
        private boolean fianlClick=false;
        private SnapRobot sr=null;
        public SnapWindow(SnapRobot sr){
         this();
         this.sr=sr;
        }
    public SnapWindow(){
    Toolkit tk=Toolkit.getDefaultToolkit();
    MAXX=100+(int)tk.getScreenSize().getWidth();
    MAXY=100+(int)tk.getScreenSize().getHeight();
    this.setSize(MAXX,MAXY);
    this.setUndecorated(true);//不用以下的 光用这句也可以 关键是放的位置
    //this.getRootPane().setWindowDecorationStyle(JRootPane.NONE); //以上两行用来去掉标题栏
    this.setVisible(true);
    //this.setResizable(false);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.addComponentListener(this);
    this.addMouseListener(this);
    AWTUtilities.setWindowOpacity(this,(float) 0.5);
      
    }
    @Override
    public void componentResized(ComponentEvent e) {
     // System.out.println("窗口大小变化"+ this.getSize());
    } @Override
    public void componentMoved(ComponentEvent e) {
    //this.setLocation(0, 0);
    //System.out.println(this.getLocationOnScreen());

    } @Override
    public void componentShown(ComponentEvent e) {
      //System.out.println("窗口出现");

    } @Override
    public void componentHidden(ComponentEvent e) {
      //System.out.println("窗口隐藏");

    }
    @Override
    public void mouseClicked(MouseEvent e) {
    // TODO Auto-generated method stub

    }
    //以上覆写的方法留着以后再用
    @Override
    public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    /*
     * (non-Javadoc)
     * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
     * 以下为 左键单击一次 确定方位 左键再单击一次 确定大小 最后一次截图
     * 右键则是恢复左键单击前的 在JPanel下不能完成很好的重绘画出准线...这个是需要加强的地方...
     */
    public void mousePressed(MouseEvent e) {
    if(e.getButton()==e.BUTTON1){
    if(!firstClick){
    x=e.getXOnScreen();
    y=e.getYOnScreen();
       this.setLocation(e.getXOnScreen(), e.getYOnScreen());//获得绝对坐标
       firstClick=true;
       return;
    }
    if(!secondClick){
       width=e.getX();
       height=e.getY();
       secondClick=true;
       this.setSize(width,height);
       return;
    }
    this.setVisible(false); // 开始截屏 防止不透明的地方被截进去
    try {
    Thread.sleep(50);
    } catch (InterruptedException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    sr.setRg(new Rectangle(x,y,width,height));
    sr.draw();
    this.setVisible(false);

    }
    if(e.getButton()==e.BUTTON3){
    if(!firstClick){            //连第一次左键都没有就直接退出(隐藏)
    this.setVisible(false);
    }
    if(secondClick){
    secondClick=false; //有第二次则退回选中坐标
    this.setSize(MAXX,MAXY);
    return;
    }
    firstClick=false; //只有按下一次的情况
    this.setLocation(0, 0);
    this.setSize(MAXX,MAXY);
    System.out.println("右键按下");
    }
    }
    @Override
    public void mouseReleased(MouseEvent e) {
    //System.out.println("鼠标放开"+e.getXOnScreen()+" "+e.getYOnScreen());

    }

    /* public void paintComponent(Graphics g){
    Color c=g.getColor();
    g.setColor(Color.RED);
    g.fillOval(50, 50, 50, 50);
    g.setColor(c);
    }

    public void paint(Graphics g){
    super.paint(g);
    Color c=g.getColor();
    g.setColor(Color.RED);
    g.fillOval(50, 50, 50, 50);
    g.setColor(c);
    }
    测试代码 无用
    */
    @Override
    public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub

    }
    @Override
    public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

    }
    }//code by cc Fair_jm 欢迎修改及使用^_^ 20120225