刚才把截屏程序放出去之后,收到了朋友 的建议,刚来这里就能认识他,很高兴,现在又改进了一下,改进的地方如下
1,修复了选择选区后,再改变大小时,当把右侧的边框拖过左侧或者左侧的边框拖到右侧或者上面的边框拖过下面,只会显示一条边框的BUG.
2,在没有截屏的时候,会有一条提示的字符串跟着鼠标走.
3,在点了截屏之后,主窗口会先隐藏起来,截完图后才会弹出来
4,去掉了截屏时的自定义鼠标显示,因为这样的话,源代码编译之后,运行会出问题,因为别人的电脑上没有我那个鼠标的图片.
5,增加了可保存的图片格式,现在可保存(JPG,GIF,PNG,BMP)格式
源代码附上,可以直接自己编译,打成JAR包.我这里也提供一下可执行JAR包的下载.
点击下载可执行的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.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()
{
this.setVisible(true);

if(get!=null)
{
save.setEnabled(true);
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
{
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();
}
}

private void doSave()
{

try
{
JFileChooser jfc=new JFileChooser(".");
jfc.addChoosableFileFilter(new JPGfilter());
jfc.addChoosableFileFilter(new PNGfilter());
jfc.addChoosableFileFilter(new GIFfilter());
jfc.addChoosableFileFilter(new BMPfilter());
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";
}

}else if(ff instanceof BMPfilter)
{

if(!ext.endsWith(".bmp"))
{
String ns=ext+".bmp";
file=new File(ns);
about="BMP";
}

}else if(ff instanceof GIFfilter)
{

if(!ext.endsWith(".gif"))
{
String ns=ext+".gif";
file=new File(ns);
about="GIF";
}
}

if(ImageIO.write(get,about,file))
{
JOptionPane.showMessageDialog(this,"保存成功!");
} else
JOptionPane.showMessageDialog(this,"保存失败!");
}

} catch(Exception exe)
{
exe.printStackTrace();
}
}