中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

java實(shí)現(xiàn)截屏功能的代碼

2018-07-20    來源:open-open

容器云強(qiáng)勢上線!快速搭建集群,上萬Linux鏡像隨意使用

抓取思路是首先抓到屏幕的整個(gè)圖象,將圖象顯示在一個(gè)JFrame中,再將JFrame全屏顯示,這樣就模擬出了一個(gè)桌面,Java也就可以獲得鼠標(biāo)的作用區(qū)域從而實(shí)現(xiàn)桌面中的小范圍截屏。

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * 用Java模擬出QQ桌面截圖功能
 * @author 五斗米 <如轉(zhuǎn)載請保留作者和出處>
 * @blog http://blog.csdn.net/mq612
 */

public class Test extends JFrame {

 private static final long serialVersionUID = -267804510087895906L;

 private JButton button = null;

 private JLabel imgLabel = null;

 public Test() {
  button = new JButton("模擬屏幕(點(diǎn)右鍵退出)");
  button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    try {
     new ScreenWindow(imgLabel);
    } catch (Exception e1) {
     JOptionPane.showConfirmDialog(null, "出現(xiàn)意外錯(cuò)誤!", "系統(tǒng)提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
    }
   }
  });
  JPanel pane = new JPanel();
  pane.setBackground(Color.WHITE);
  imgLabel = new JLabel();
  pane.add(imgLabel);
  JScrollPane spane = new JScrollPane(pane);
  this.getContentPane().add(button, BorderLayout.NORTH);
  this.getContentPane().add(spane);
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(300, 200);
  this.setLocationRelativeTo(null);
  this.setVisible(true);
 }

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

class ScreenWindow extends JFrame {

 private static final long serialVersionUID = -3758062802950480258L;

 private boolean isDrag = false;

 private int x = 0;

 private int y = 0;

 private int xEnd = 0;

 private int yEnd = 0;

 public ScreenWindow(final JLabel imgLabel) throws AWTException, InterruptedException {
  Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
  JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0, 0, screenDims.width, screenDims.height)));
  label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
  label.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON3) {
     dispose();
    }
   }

   public void mousePressed(MouseEvent e) {
    x = e.getX();
    y = e.getY();
   }

   public void mouseReleased(MouseEvent e) {
    if (isDrag) {
     xEnd = e.getX();
     yEnd = e.getY();
     if(x > xEnd){
      int temp = x;
      x = xEnd;
      xEnd = temp;
     }
     if(y > yEnd){
      int temp = y;
      y = yEnd;
      yEnd = temp;
     }
     try {
      imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x, y, xEnd - x, yEnd - y)));
     } catch (Exception ex) {
      JOptionPane.showConfirmDialog(null, "出現(xiàn)意外錯(cuò)誤!", "系統(tǒng)提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
     }
     dispose();
    }
   }
  });
  label.addMouseMotionListener(new MouseMotionListener() {
   public void mouseDragged(MouseEvent e) {
    if(!isDrag)
     isDrag = true;
   }

   public void mouseMoved(MouseEvent e) {
    /** 拖動(dòng)過程的虛線選取框需自己實(shí)現(xiàn) */
   }
  });
  this.setUndecorated(true);
  this.getContentPane().add(label);
  this.setSize(screenDims.width, screenDims.height);
  this.setVisible(true);
  this.setExtendedState(JFrame.MAXIMIZED_BOTH);
 }
}

class ScreenImage {

 public static Image getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {
  Robot robot = new Robot();
  Image screen = robot.createScreenCapture(new Rectangle(x, y, w, h)).getScaledInstance(w, h, Image.SCALE_SMOOTH);
  MediaTracker tracker = new MediaTracker(new Label());
  tracker.addImage(screen, 1);
  tracker.waitForID(0);
  return screen;
 }
}

標(biāo)簽: isp seo

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:JavaScript生成隨機(jī)字符

下一篇:jdbc數(shù)據(jù)庫連接代碼