呵呵,昨天也看到朋友的留言,又增加了一些功能,
1,增加了一个把截到的图片复制到系统的剪帖板,这样一来就可以直接粘帖到word,photoshop,画图等程序里面了
2,增加了每截一张图片都会新开一个tab来放它,并且tab里面各自有各自的操作方法.
点击下载源文件和可执行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.datatransfer.DataFlavor;
import
java.awt.datatransfer.Transferable;
import
java.awt.datatransfer.UnsupportedFlavorException;
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;
private
JPanel c;
private
BufferedImage get;
private
JTabbedPane jtp;
//
一个放置很多份图片
private
int
index;
//
一个一直会递增的索引,用于标认图片
private
JRadioButton java,system;
//
JAVA界面,系统界面
/**
Creates a new instance of CaptureScreen
*/
public
CaptureScreen() {
super
(
"
屏幕截取软件(第三版)
"
);
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch
(Exception exe){
exe.printStackTrace();
}
initWindow();
initOther();
}
private
void
initOther(){
jtp
=
new
JTabbedPane(JTabbedPane.TOP,JTabbedPane.SCROLL_TAB_LAYOUT);
}
private
void
initWindow(){
start
=
new
JButton(
"
开始截取
"
);
cancel
=
new
JButton(
"
退出
"
);
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(cancel);
buttonJP.setBorder(BorderFactory.createTitledBorder(
"
公共操作区
"
));
JPanel jp
=
new
JPanel();
//
放两个单选按钮的面板
jp.add(java
=
new
JRadioButton(
"
java界面
"
));
jp.add(system
=
new
JRadioButton(
"
系统界面
"
,
true
));
java.addActionListener(
this
);
system.addActionListener(
this
);
jp.setBorder(BorderFactory.createTitledBorder(
"
界面风格
"
));
ButtonGroup bg
=
new
ButtonGroup();
bg.add(java);
bg.add(system);
JPanel all
=
new
JPanel();
all.add(jp);
all.add(buttonJP);
this
.getContentPane().add(c,BorderLayout.CENTER);
this
.getContentPane().add(all,BorderLayout.SOUTH);
this
.setSize(
500
,
400
);
this
.setLocationRelativeTo(
null
);
this
.setVisible(
true
);
this
.setAlwaysOnTop(
true
);
this
.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private
void
updates(){
this
.setVisible(
true
);
if
(get
!=
null
){
//
如果索引是0,则表示一张图片都没有被加入过,
//
则要清除当前的东西,重新把tabpane放进来
if
(index
==
0
){
c.removeAll();
c.add(jtp,BorderLayout.CENTER);
}
else
{
//
否则的话,直接对tabpane添加面板就可以了
//
就什么都不用做了
}
PicPanel pic
=
new
PicPanel(get);
jtp.addTab(
"
图片
"
+
(
++
index),pic);
jtp.setSelectedComponent(pic);
SwingUtilities.updateComponentTreeUI(c);
}
}
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();
}
}
/**
*公用的处理保存图片的方法
*这个方法不再私有了
*/
public
void
doSave(BufferedImage get){
try
{
if
(get
==
null
){
JOptionPane.showMessageDialog(
this
,
"
图片不能为空!!
"
,
"
错误
"
,JOptionPane.ERROR_MESSAGE);
return
;
}
JFileChooser jfc
=
new
JFileChooser(
"
.
"
);
jfc.addChoosableFileFilter(
new
GIFfilter());
jfc.addChoosableFileFilter(
new
BMPfilter());
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
"
;
}
}
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();
}
}
/**
*公共的处理把当前的图片加入剪帖板的方法
*/
public
void
doCopy(
final
BufferedImage image){
try
{
if
(get
==
null
){
JOptionPane.showMessageDialog(
this
,
"
图片不能为空!!
"
,
"
错误
"
,JOptionPane.ERROR_MESSAGE);
return
;
}
Transferable trans
=
new
Transferable(){
public
DataFlavor[] getTransferDataFlavors() {
return
new
DataFlavor[] { DataFlavor.imageFlavor };
}
public
boolean
isDataFlavorSupported(DataFlavor flavor) {
return
DataFlavor.imageFlavor.equals(flavor);
}
public
Object getTransferData(DataFlavor flavor)
throws
UnsupportedFlavorException, IOException {
if
(isDataFlavorSupported(flavor))
return
image;
throw
new
UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans,
null
);
JOptionPane.showMessageDialog(
this
,
"
已复制到系统粘帖板!!
"
);
}
catch
(Exception exe){
exe.printStackTrace();
JOptionPane.showMessageDialog(
this
,
"
复制到系统粘帖板出错!!
"
,
"
错误
"
,JOptionPane.ERROR_MESSAGE);
}
}
//
处理关闭事件
private
void
doClose(Component c){
jtp.remove(c);
c
=
null
;
System.gc();
}
public
void
actionPerformed(ActionEvent ae){
Object source
=
ae.getSource();
if
(source
==
start){
doStart();
}
else
if
(source
==
cancel){
System.exit(
0
);
}
else
if
(source
==
java){
try
{
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(
this
);
}
catch
(Exception exe){
exe.printStackTrace();
}
}
else
if
(source
==
system){
try
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(
this
);
}
catch
(Exception exe){
exe.printStackTrace();
}
}
}
//
一个内部类,它表示一个面板,一个可以被放进tabpane的面板
//
也有自己的一套处理保存和复制的方法
private
class
PicPanel
extends
JPanel
implements
ActionListener{
JButton save,copy,close;
//
表示保存,复制,关闭的按钮
BufferedImage get;
//
得到的图片
public
PicPanel(BufferedImage get){
super
(
new
BorderLayout());
this
.get
=
get;
initPanel();
}
private
void
initPanel(){
save
=
new
JButton(
"
保存(S)
"
);
copy
=
new
JButton(
"
复制到剪帖板(C)
"
);
close
=
new
JButton(
"
关闭(X)
"
);
save.setMnemonic(
'
S
'
);
copy.setMnemonic(
'
C
'
);
close.setMnemonic(
'
X
'
);
JPanel buttonPanel
=
new
JPanel();
buttonPanel.add(copy);
buttonPanel.add(save);
buttonPanel.add(close);
JLabel icon
=
new
JLabel(
new
ImageIcon(get));
this
.add(
new
JScrollPane(icon),BorderLayout.CENTER);
this
.add(buttonPanel,BorderLayout.SOUTH);
save.addActionListener(
this
);
copy.addActionListener(
this
);
close.addActionListener(
this
);
}
public
void
actionPerformed(ActionEvent e) {
Object source
=
e.getSource();
if
(source
==
save){
doSave(get);
}
else
if
(source
==
copy){
doCopy(get);
}
else
if
(source
==
close){
get
=
null
;
doClose(
this
);
}
}
}
//
保存BMP格式的过滤器
private
class
BMPfilter
extends
javax.swing.filechooser.FileFilter{
public
BMPfilter(){
}
public
boolean
accept(File file){
if
(file.toString().toLowerCase().endsWith(
"
.bmp
"
)
||
file.isDirectory()){
return
true
;
}
else
return
false
;
}
public
String getDescription(){
return
"
*.BMP(BMP图像)
"
;
}
}
//
保存JPG格式的过滤器
private
class
JPGfilter
extends
javax.swing.filechooser.FileFilter{
public
JPGfilter(){
}
public
boolean
accept(File file){
if
(file.toString().toLowerCase().endsWith(
"
.jpg
"
)
||
file.isDirectory()){
return
true
;
}
else
return
false
;
}
public
String getDescription(){
return
"
*.JPG(JPG图像)
"
;
}
}
//
保存GIF格式的过滤器
private
class
GIFfilter
extends
javax.swing.filechooser.FileFilter{
public
GIFfilter(){
}
public
boolean
accept(File file){
if
(file.toString().toLowerCase().endsWith(
"
.gif
"
)
||
file.isDirectory()){
return
true
;
}
else
return
false
;
}
public
String getDescription(){
return
"
*.GIF(GIF图像)
"
;
}
}
//
保存PNG格式的过滤器
private
class
PNGfilter
extends
javax.swing.filechooser.FileFilter{
public
boolean
accept(File file){
if
(file.toString().toLowerCase().endsWith(
"
.png
"
)
||
file.isDirectory()){