JAVA截图一

来源:互联网 发布:必修三数学算法程序 编辑:程序博客网 时间:2024/06/11 14:59
import java.awt.AWTException;import java.awt.Color;import java.awt.Dimension;import java.awt.Graphics;import java.awt.GraphicsDevice;import java.awt.GraphicsEnvironment;import java.awt.Rectangle;import java.awt.Robot;import java.awt.Toolkit;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionAdapter;import java.awt.image.BufferedImage;import java.awt.image.RescaleOp;import java.io.File;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.filechooser.FileSystemView;/** * 功能:java截图 运行后将当前屏幕截取,并最大化显示。 拖拽鼠标,选择自己需要的部分。 按Esc键保存图片到桌面,并退出程序。 * 双击鼠标左键,保存图片到桌面,并退出程序 点击右上角(没有可见的按钮),退出程序,不保存图片。 点击右键,退出程序,不保存图片。 *  * @author 花新昌 */public class SnapshotTest1 {public static void main(String[] args) {// 全屏运行RectD rd = new RectD();GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();gd.setFullScreenWindow(rd);}}class RectD extends JFrame {private static final long serialVersionUID = 1L;int orgx, orgy, endx, endy;// 鼠标按下和释放时x、y轴坐标Dimension d = Toolkit.getDefaultToolkit().getScreenSize();// 获得屏幕大小BufferedImage image;// 存储整个屏幕BufferedImage tempImage;// 缓存BufferedImage saveImage;// 保存(截取的区域)Graphics g;@Overridepublic void paint(Graphics g) {RescaleOp ro = new RescaleOp(0.8f, 0, null);// 构造一个具有所希望的缩放因子和偏移量的新// RescaleOptempImage = ro.filter(image, null);// 对源 BufferedImage对象image进行重缩放g.drawImage(tempImage, 0, 0, this);}public RectD() {snapshot();setVisible(true);// 设置窗口可见setSize(d);// 最大化窗口setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 默认关闭方式this.addMouseListener(new MouseAdapter() {public void mousePressed(MouseEvent e) {orgx = e.getX();orgy = e.getY();}});this.addMouseMotionListener(new MouseMotionAdapter() {public void mouseDragged(MouseEvent e) {endx = e.getX();endy = e.getY();g = getGraphics();g.drawImage(tempImage, 0, 0, RectD.this);int x = Math.min(orgx, endx);int y = Math.min(orgy, endy);int width = Math.abs(endx - orgx) + 1;int height = Math.abs(endy - orgy) + 1;// 加上1,防止width或height为0g.setColor(Color.BLUE);g.drawRect(x - 1, y - 1, width + 1, height + 1);// 减1,加1都是为了防止图片将矩形框覆盖掉saveImage = image.getSubimage(x, y, width, height);g.drawImage(saveImage, x, y, RectD.this);}});this.addKeyListener(new KeyAdapter() {public void keyReleased(KeyEvent e) {// 按Esc键if (e.getKeyCode() == 27) {if (saveImage != null) {saveToFile();// 保存图片}System.exit(0);// 退出}}});this.addMouseListener(new MouseListener() {@Overridepublic void mouseClicked(MouseEvent e) {// 单击(双击)组件时调用// TODO Auto-generated method stub// 双击鼠标左键if (e.getClickCount() == 2) {if (saveImage != null) {// 如果截图了则保存图片到桌面saveToFile();}System.exit(0);// 退出}}@Overridepublic void mouseEntered(MouseEvent e) {// 进入组件时调用// TODO Auto-generated method stub}@Overridepublic void mouseExited(MouseEvent e) {// 离开组件时调用// TODO Auto-generated method stub}@Overridepublic void mousePressed(MouseEvent e) {// 在组件上按下鼠标按键时调用// TODO Auto-generated method stub// 点击鼠标右键则退出程序if (e.getModifiers() == MouseEvent.BUTTON3_MASK) {System.exit(0);}}@Overridepublic void mouseReleased(MouseEvent e) {// 在组件上释放鼠标按钮时调用// TODO Auto-generated method stub}});}// 保存图片到桌面,图片名称格式为yyyymmddHHmmss.jpgpublic void saveToFile() {SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");String name = sdf.format(new Date());// 格式转化获得图片名File path = FileSystemView.getFileSystemView().getHomeDirectory();// 获得桌面路径String format = "jpg";File f = new File(path + File.separator + name + "." + format);try {ImageIO.write(saveImage, format, f);// 生成图片} catch (IOException e) {// 抛出异常e.printStackTrace();}}public void snapshot() {try {Robot robot = new Robot();// 在基本屏幕坐标系中构造一个 Robot对象Dimension d = Toolkit.getDefaultToolkit().getScreenSize();image = robot.createScreenCapture(new Rectangle(0, 0, d.width,d.height));// 获得整个屏幕} catch (AWTException e) {e.printStackTrace();}}}