Java實現了任意位置截屏(仿QQ截圖)__java

來源:互聯網
上載者:User

最近剛剛學完java,就做了這個小程式。當然,也是通過百度尋找資料,最後做出了這一個小程式。

功能:

點擊截屏按鈕,就開始截屏,在螢幕上畫出一個地區之後,就可以通過雙擊將圖片儲存到案頭。如果不想截屏,就右鍵滑鼠,退出程式。

我的是win10系統,所以案頭的地址是:C:\\Users\\Administrator\\Desktop。

這個小程式可以實現向左上方或者右下角方向的截屏。

這個小程式,每截一次屏,就退出了程式,能力有限,所以想再次截屏,就需要再次開啟。

下面的是代碼:

import javax.imageio.ImageIO;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;public class DrawRectTest {public static void main(String[] args) {new NewFrame();}}class NewFrame extends JFrame{private static final long serialVersionUID = 1L;/* * 建立一個小的視窗。點擊按鈕來截屏。 */JButton button;NewFrame(){setVisible(true);setLayout(new FlowLayout());setBounds(1000, 600, 100, 100);setResizable(false);button = new JButton("截圖");add(button);button.addActionListener(new ActionListener(){//滑鼠點擊按鈕,new 一個ScreenFrame,設定可見,public void actionPerformed(ActionEvent e) {ScreenFrame sf = new ScreenFrame();sf.setAlwaysOnTop(true);sf.setUndecorated(true);sf.setVisible(true);}});addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}}class ScreenFrame extends JFrame{private static final long serialVersionUID = 2L;/* * 建立一個全屏的視窗,將全屏的映像放在JFrame的視窗上,以供來截屏。 */Dimension di = Toolkit.getDefaultToolkit().getScreenSize();ScreenFrame(){//設定大小,即全屏setSize(di);//返回此表單的 contentPane對象 getContentPane().add(new DrawRect());}class DrawRect extends JPanel implements MouseMotionListener, MouseListener{private static final long serialVersionUID = 3L;/* * 將全屏的映像放在JPanel 上, 可以通過new DrawRect來獲得JPanel,並且JPanel上有全屏映像 */int sx, sy, ex, ey;int count = 1;File file = null;BufferedImage image, getImage;boolean flag = true;public DrawRect(){try{//擷取全屏映像資料,返回給imageRobot robot = new Robot();image = robot.createScreenCapture(new Rectangle(0, 0, di.width, di.height));}catch(Exception e){throw new RuntimeException("截圖失敗");}//添加 滑鼠活動事件addMouseListener(this);addMouseMotionListener(this);}//重寫paintComponent,通過repaint 顯示出來截屏的範圍public void paintComponent(Graphics g){g.drawImage(image, 0, 0, di.width, di.height, this);g.setColor(Color.blue);if(sx < ex && sy < ey)//右下角g.drawRect(sx, sy, ex - sx, ey - sy);else                 //左上方g.drawRect(ex, ey, sx - ex, sy - ey);}//以下都是滑鼠事件public void mouseClicked(MouseEvent e){if(e.getButton() == MouseEvent.BUTTON3)//右鍵退出程式System.exit(0);else if(e.getClickCount() == 2)   //雙擊截屏{try{cutScreens();}catch(Exception ex){ex.printStackTrace();}}}//自訂截屏函數private void cutScreens() throws Exception{Robot ro = new Robot();if(sx < ex && sy < ey)//右下角getImage = ro.createScreenCapture(new Rectangle(sx, sy,ex - sx, ey - sy));else                  //左上方getImage = ro.createScreenCapture(new Rectangle(ex, ey,sx - ex, sy - ey));String name = "jietu" + count + ".bmp";file = new File("C:\\Users\\Administrator\\Desktop", name);while(file.exists()){String na = "jietu" + (count++) + ".bmp";file = new File("C:\\Users\\Administrator\\Desktop", na);}ImageIO.write(getImage, "bmp", file);System.exit(0);}public void mousePressed(MouseEvent e){if(flag){sx = e.getX();sy = e.getY();}}public void mouseReleased(MouseEvent e){flag = false;}public void mouseEntered(MouseEvent e) {}public void mouseExited(MouseEvent e){}//滑鼠移動中,通過repaint 畫出要截屏的範圍public void mouseDragged(MouseEvent e) {ex = e.getX();ey = e.getY();repaint();}public void mouseMoved(MouseEvent e) {}}}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.