最近剛剛學完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) {}}}