<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-46  評論-64  文章-2  trackbacks-0
    今天稍微花了點時間實現(xiàn)了一個java的屏幕截圖程序,功能簡單,支持全屏截圖和選擇截圖

    用JSmooth做了個.exe程序,直接運行就可以了,

    附:

    程序下載 http://www.tkk7.com/Files/jht/MyScreenSnap.zip

    JRE1.4版本的可執(zhí)行程序 http://www.tkk7.com/Files/jht/MyScreenSnap_jre1.4.zip

    關(guān)鍵的有幾點
    一、做一個透明效果的窗體
    二、調(diào)用Robot類的捕獲屏幕方法

    代碼比較粗糙,暫時就這樣。

    代碼如下:
    package?cn.heapstack.MyScreenSnap;

    import?java.awt.AWTException;
    import?java.awt.Color;
    import?java.awt.Dimension;
    import?java.awt.DisplayMode;
    import?java.awt.FlowLayout;
    import?java.awt.GraphicsDevice;
    import?java.awt.GraphicsEnvironment;
    import?java.awt.Rectangle;
    import?java.awt.Robot;
    import?java.awt.event.ActionEvent;
    import?java.awt.event.ActionListener;
    import?java.awt.image.BufferedImage;
    import?java.io.File;
    import?java.io.IOException;

    import?javax.imageio.ImageIO;
    import?javax.swing.JButton;
    import?javax.swing.JFrame;
    import?javax.swing.JPanel;

    public?class?MenuFrame?{
    ????
    ????
    private?JFrame?frame;
    ????
    private?TranslucentFrame?tframe;
    ????
    private?JPanel?panel;
    ????
    private?JButton?button1;
    ????
    private?JButton?button2;
    ????
    private?JButton?button3;
    ????
    private?FormListener?formListener;
    ????
    private?Robot?robot;
    ????
    ????
    public?static?????GraphicsEnvironment?graphenv?=?GraphicsEnvironment.getLocalGraphicsEnvironment?();
    ????
    public?static?????GraphicsDevice?[]?screens?=?graphenv.getScreenDevices?();????
    ????
    public?static?????DisplayMode?mode?=?screens?[0].getDisplayMode?();????
    ????
    ????
    public?MenuFrame()
    ????
    {
    ????????initComponents();
    ????????
    ????}

    ????
    ????
    public?void?initComponents()
    ????
    {
    ????????frame?
    =?new?JFrame("MyScreenSnap?Version:1.0?Author:jacky.jihao@gmail.com");
    ????????
    ????????tframe?
    =?new?TranslucentFrame();
    ????????panel?
    =?new?JPanel();
    ????????
    ????????
    try?{
    ????????????
    ????????????robot?
    =?new?Robot();
    ????????}
    ?catch?(AWTException?e)?{
    ????????????e.printStackTrace();
    ????????}
    ?
    ????????
    ????????button1?
    =?new?JButton("Capture?Full?Screen");
    ????????button2?
    =?new?JButton("Capture?Selected?Screen");
    ????????button3?
    =?new?JButton("Exit");
    ????????formListener?
    =?new?FormListener();
    ????????button1.addActionListener(formListener);????
    ????????button2.addActionListener(formListener);????
    ????????button3.addActionListener(formListener);????
    ????????
    ????????
    //fullScreenFrame.setVisible(false);
    ????????tframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ????????tframe.setUndecorated(
    true);
    ????????tframe.setAlwaysOnTop(
    true);
    ????????tframe.setSize(
    2000,2000);
    ????????
    ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ????????
    ????????frame.setLayout(
    null);
    ????????Dimension?size?
    =?new?Dimension(500,80);
    ????????frame.setPreferredSize(size?);
    ????????frame.setSize(size);
    ????????frame.setLocation(
    300,300);
    ????????frame.setBackground(Color.black);
    ????????
    ????????panel.setLocation(
    0,?0);
    ????????panel.setSize(size);
    ????????panel.setPreferredSize(size);
    ????????panel.setLayout(
    new?FlowLayout());
    ????????
    ????????panel.setOpaque(
    false);
    ????????
    ????????panel.add(button1);
    ????????panel.add(button2);
    ????????panel.add(button3);
    ????????
    ????????
    ????????frame.add(panel);
    ????????
    ????????frame.pack();
    ????????
    ????????frame.show();
    ????????
    ????}


    ????
    class?FormListener?implements?ActionListener
    ????
    {
    ????????
    ????????
    public?void?actionPerformed(ActionEvent?e)?{
    ????????????
    if(e.getSource()?==?button1)
    ????????????
    {
    ????????????????System.out.println(
    "Just?capture?the?screen?using?a?robot");
    ????????????????frame.hide();
    ????????????????Rectangle?bounds?
    =?new?Rectangle?(0,?0,?mode.getWidth?(),?mode.getHeight?());????
    ????????????????
    ????????????????BufferedImage?bimg?
    =?robot.createScreenCapture(bounds);
    ????????????????????????????????
    ????????????????String?fileName?
    =?String.valueOf(System.currentTimeMillis())+".png";
    ????????????????File?outputFile?
    =?new?File(fileName);
    ????????????????
    try?
    ????????????????
    {
    ????????????????????ImageIO.write(bimg,?
    "png",?outputFile);
    ????????????????}
    ?
    ????????????????
    catch?(IOException?e1)?
    ????????????????
    {
    ????????????????????e1.printStackTrace();
    ????????????????}


    ????????????????frame.show();
    ????????????????System.out.println(
    "File?save?as?"+fileName);
    ????????????????
    ????????????}

    ????????????
    else?if(e.getSource()?==?button2)
    ????????????
    {
    ????????????????System.out.println(
    "Create?a?full?screen?frame");
    ????????????????
    if(!tframe.isShowing())
    ????????????????
    {
    ????????????????????frame.hide();
    ????????????????????tframe.updateBackground();
    ????????????????????tframe.show();
    ????????????????????frame.show();
    ????????????????????frame.toBack();
    ????????????????}

    ????????????????
    else
    ????????????????????tframe.show();
    ????????????????
    ????????????}

    ????????????
    else?if(e.getSource()?==?button3)
    ????????????
    {
    ????????????????System.exit(
    0);
    ????????????}

    ????????????
    ????????}

    ????????
    ????}

    ????
    ????
    public?static?void?main(String[]?args)?{
    ????????
    new?MenuFrame();
    ????}


    }


    package?cn.heapstack.MyScreenSnap;

    import?java.awt.AWTException;
    import?java.awt.Color;
    import?java.awt.Dimension;
    import?java.awt.Graphics;
    import?java.awt.Point;
    import?java.awt.Rectangle;
    import?java.awt.Robot;
    import?java.awt.Toolkit;
    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?java.awt.event.WindowEvent;
    import?java.awt.event.WindowFocusListener;
    import?java.awt.image.BufferedImage;
    import?java.awt.image.ImageFilter;
    import?java.io.File;
    import?java.io.FileFilter;
    import?java.io.IOException;
    import?java.util.Iterator;

    import?javax.imageio.IIOImage;
    import?javax.imageio.ImageIO;
    import?javax.imageio.ImageWriteParam;
    import?javax.imageio.ImageWriter;
    import?javax.imageio.stream.ImageOutputStream;
    import?javax.swing.ImageIcon;
    import?javax.swing.JFileChooser;
    import?javax.swing.JFrame;
    import?javax.swing.JOptionPane;

    public?class?TranslucentFrame?extends?JFrame?implements?ComponentListener,
    ????????WindowFocusListener,?MouseListener,?MouseMotionListener?
    {

    ????
    private?boolean?flag_prepare?=?true;

    ????
    private?BufferedImage?background;

    ????
    private?Robot?robot;

    ????
    private?Dimension?size;

    ????
    private?Point?startPoint;

    ????
    private?Point?lastPoint;

    ????
    private?int?width?=?0;
    ????
    private?int?w?=?0;
    ????
    private?int?h?=?0;
    ????
    private?int?height?=?0;

    ????
    public?TranslucentFrame()?{
    ????????init();
    ????}


    ????
    private?void?init()?{
    ????????
    try?{
    ????????????robot?
    =?new?Robot();
    ????????????size?
    =?Toolkit.getDefaultToolkit().getScreenSize();
    ????????}
    ?catch?(AWTException?e)?{
    ????????????e.printStackTrace();
    ????????}

    ????????startPoint?
    =?new?Point();
    ????????lastPoint?
    =?new?Point();
    ????????
    this.addWindowFocusListener(this);
    ????????
    this.addComponentListener(this);
    ????????
    this.addMouseListener(this);
    ????????
    this.addMouseMotionListener(this);

    ????????
    this.updateBackground();
    ????}


    ????
    public?void?updateBackground()?{
    ????????background?
    =?robot.createScreenCapture(new?Rectangle(0,?0,?(int)?size
    ????????????????.getWidth(),?(
    int)?size.getHeight()));
    ????}


    ????
    public?void?refresh()?{

    ????????
    this.repaint();
    ????}


    ????
    public?void?repaint()?{
    ????????Graphics?g?
    =?this.getGraphics();

    ????????g.setColor(Color.red);
    ????????w?
    =?lastPoint.x?-?startPoint.x;
    ????????h?
    =?lastPoint.y?-?startPoint.y;
    ????????width?
    =?Math.abs(w);
    ????????height?
    =?Math.abs(h);

    ????????
    //?don't?need?to?clear?Rect?now,?just?paint?the?background,?it?feels
    ????????
    //?good
    ????????
    //?g.clearRect(startPoint.x,?startPoint.y,?width,?height);

    ????????
    this.paintComponents(g);

    ????????
    if?(!this.flag_prepare)?{

    ????????????
    if?(((w)?<?0)?&&?((h)?<?0))?{
    ????????????????g.drawRect(lastPoint.x,?lastPoint.y,?width,?height);

    ????????????}
    ?else?if?(((w)?>?0)?&&?((h)?<?0))?{
    ????????????????g.drawRect(startPoint.x,?lastPoint.y,?width,?height);

    ????????????}
    ?else?if?(((w)?<?0)?&&?((h)?>?0))?{
    ????????????????g.drawRect(lastPoint.x,?startPoint.y,?width,?height);

    ????????????}
    ?else?if?(((w)?>?0)?&&?((h)?>?0))?{
    ????????????????g.drawRect(startPoint.x,?startPoint.y,?width,?height);

    ????????????}

    ????????}
    ?else?{
    ????????????g.drawLine(
    0,?lastPoint.y,?size.width,?lastPoint.y);
    ????????????g.drawLine(lastPoint.x,?
    0,?lastPoint.x,?size.height);
    ????????}

    ????}


    ????
    public?void?paintComponents(Graphics?g)?{
    ????????Point?pos?
    =?this.getLocationOnScreen();
    ????????Point?offset?
    =?new?Point(-pos.x,?-pos.y);
    ????????g.drawImage(background,?offset.x,?offset.y,?
    null);
    ????}


    ????
    private?static?final?long?serialVersionUID?=?3690836343560995785L;

    ????
    public?static?void?main(String[]?args)?{
    ????????TranslucentFrame?frame?
    =?new?TranslucentFrame();
    ????????frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    ????????frame.setUndecorated(
    true);
    ????????frame.setAlwaysOnTop(
    true);
    ????????frame.setSize(
    2000,?2000);
    ????????frame.show();

    ????}


    ????
    public?void?componentHidden(ComponentEvent?e)?{
    ????}


    ????
    public?void?componentMoved(ComponentEvent?e)?{
    ????????
    this.refresh();
    ????}


    ????
    public?void?componentResized(ComponentEvent?e)?{
    ????}


    ????
    public?void?componentShown(ComponentEvent?e)?{
    ????????
    this.refresh();
    ????}


    ????
    public?void?windowGainedFocus(WindowEvent?e)?{
    ????????
    this.refresh();
    ????}


    ????
    public?void?windowLostFocus(WindowEvent?e)?{
    ????}


    ????
    public?void?mouseMoved(MouseEvent?e)?{
    ????????
    //?System.out.println(e.getX()?+?":"?+?e.getY());
    ????????this.lastPoint.x?=?e.getX();
    ????????
    this.lastPoint.y?=?e.getY();
    ????????repaint();
    ????}


    ????
    public?void?mousePressed(MouseEvent?e)?{
    ????????System.out.println(
    "Mouse?pressed?,?set?the?start?point,x:"?+?e.getX()
    ????????????????
    +?"?y:"?+?e.getY());
    ????????
    if?(e.getButton()?==?MouseEvent.BUTTON3)?{
    ????????????System.out.println(
    "Get?right?mouse");
    ????????????
    this.hide();
    ????????}
    ?else?{
    ????????????
    this.flag_prepare?=?false;
    ????????????
    this.startPoint.x?=?e.getX();
    ????????????
    this.startPoint.y?=?e.getY();
    ????????}

    ????}


    ????
    public?void?mouseReleased(MouseEvent?e)?{
    ????????System.out.println(
    "Mouse?released?,?set?the?last?point,x:"?+?e.getX()
    ????????????????
    +?"?y:"?+?e.getY());
    ????????
    this.flag_prepare?=?true;
    ????????
    //?save?the?picture
    ????????if?(e.getButton()?==?MouseEvent.BUTTON1)?{
    ????????????
    ????????????BufferedImage?bimg?
    =?null;
    ????????????
    if?(((w)?<?0)?&&?((h)?<?0))?{
    ????????????????
    //g.drawRect(lastPoint.x,?lastPoint.y,?width,?height);
    ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(lastPoint.x+1,?lastPoint.y+1,?width-1,?height-1));

    ????????????}
    ?else?if?(((w)?>?0)?&&?((h)?<?0))?{
    ????????????????
    //g.drawRect(startPoint.x,?lastPoint.y,?width,?height);
    ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(startPoint.x+1,?lastPoint.y+1,?width-1,?height-1));

    ????????????}
    ?else?if?(((w)?<?0)?&&?((h)?>?0))?{
    ????????????????
    //g.drawRect(lastPoint.x,?startPoint.y,?width,?height);
    ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(lastPoint.x+1,?startPoint.y+1,?width-1,?height-1));

    ????????????}
    ?else?if?(((w)?>?0)?&&?((h)?>?0))?{
    ????????????????
    //g.drawRect(startPoint.x,?startPoint.y,?width,?height);
    ????????????????bimg?=?robot.createScreenCapture(new?Rectangle(startPoint.x+1,?startPoint.y+1,?width-1,?height-1));

    ????????????}

    ????????????
    ????????????
    ????????????JFileChooser?fcSave?
    =?new?JFileChooser();
    ????????????fcSave.setCurrentDirectory(
    new?File(System.getProperty("user.dir")));
    ????????????fcSave.setSelectedFile(
    null);
    ????????????
    if?(fcSave.showSaveDialog(this)?!=?JFileChooser.APPROVE_OPTION)
    ????????????????
    return;

    ????????????File?file?
    =?fcSave.getSelectedFile();
    ????????????String?path?
    =?file.getAbsolutePath().toLowerCase();
    ????????????
    if?(!path.endsWith(".png"))
    ????????????????file?
    =?new?File(path?+=?".png");
    ????????????
    ????????????
    ????????????
    try?{
    ????????????????ImageIO.write(bimg,?
    "png",?file);
    ????????????}
    ?catch?(IOException?e1)?{
    ????????????????e1.printStackTrace();
    ????????????}

    ????????}


    ????????
    if?(this.isShowing())
    ????????????repaint();
    ????}


    ????
    public?void?mouseClicked(MouseEvent?e)?{

    ????}


    ????
    public?void?mouseEntered(MouseEvent?e)?{

    ????}


    ????
    public?void?mouseExited(MouseEvent?e)?{

    ????}


    ????
    public?void?mouseDragged(MouseEvent?e)?{
    ????????
    this.lastPoint.x?=?e.getX();
    ????????
    this.lastPoint.y?=?e.getY();
    ????????repaint();
    ????}


    }


    posted on 2007-03-29 19:19 jht 閱讀(1684) 評論(5)  編輯  收藏 所屬分類: J2SESwing Tips

    評論:
    # re: MyScreenSnap 一個簡單的截圖程序 2007-04-01 09:24 | zhenting
    對jdk版本有沒有什么要求?
    我的是1.4  回復(fù)  更多評論
      
    # re: MyScreenSnap 一個簡單的截圖程序 2007-09-13 15:09 | jht
    稍微修改了一下,做了個支持jre1.4的  回復(fù)  更多評論
      
    # re: MyScreenSnap 一個簡單的截圖程序 2007-09-13 15:37 | 千里冰封
    呵呵,支持一下,不錯:)  回復(fù)  更多評論
      
    # re: MyScreenSnap 一個簡單的截圖程序 2009-11-30 09:58 | 消毒粉
    能否給加點注釋,看的好看點!  回復(fù)  更多評論
      
    # re: MyScreenSnap 一個簡單的截圖程序 2011-09-09 08:14 | tb
    有注釋么   回復(fù)  更多評論
      
    主站蜘蛛池模板: 一本到卡二卡三卡免费高| 亚洲精品国产字幕久久不卡| 永久免费精品影视网站| 亚洲一区二区中文| 在线亚洲午夜片AV大片| 精品熟女少妇av免费久久| 亚洲AV无码国产在丝袜线观看| 污污的视频在线免费观看| 亚洲人成中文字幕在线观看| 免费H网站在线观看的| 免费的黄网站男人的天堂| 亚洲国产精品自在线一区二区| 免费观看男人免费桶女人视频| 中文字幕一区二区三区免费视频| 亚洲欧洲高清有无| 亚洲AⅤ永久无码精品AA| 中文字幕天天躁日日躁狠狠躁免费| 噜噜综合亚洲AV中文无码| 久久亚洲国产伦理| 又粗又黄又猛又爽大片免费| 99久久久国产精品免费蜜臀| 色屁屁在线观看视频免费| 亚洲国语在线视频手机在线| 久久亚洲精品无码播放| 成人免费午夜视频| 免费A级毛片无码A∨ | 亚洲国产理论片在线播放| 亚洲日韩涩涩成人午夜私人影院| h视频在线免费看| 中文在线观看国语高清免费| 久久亚洲精品无码av| 亚洲欧洲国产视频| 亚洲乱码一区二区三区在线观看| 日本免费一二区在线电影| 日本人的色道免费网站| 免费国产在线视频| 51午夜精品免费视频| 美女被免费视频网站a| 亚洲小说图区综合在线| 蜜芽亚洲av无码精品色午夜| 亚洲人成影院在线无码按摩店|