Java類比QQ案頭截圖功能實現方法_java

來源:互聯網
上載者:User

本文執行個體講述了Java類比QQ案頭截圖功能實現方法。分享給大家供大家參考。具體如下:

QQ的案頭截圖功能非常方便,去年曾用Java類比過一個,現整理出來。
本方法首先需要抓到螢幕的整個圖象,將圖象顯示在一個JFrame中,再將JFrame全螢幕顯示,這樣就類比出了一個案頭,Java也就可以獲得滑鼠的作用地區從而實現案頭中的小範圍截屏。

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案頭截圖功能 */public class Test extends JFrame { private static final long serialVersionUID = -267804510087895906L; private JButton button = null; private JLabel imgLabel = null; public Test() { button = new JButton("類比螢幕(點右鍵退出)"); button.addActionListener(new ActionListener() {  public void actionPerformed(ActionEvent e) {  try {   new ScreenWindow(imgLabel);  } catch (Exception e1) {   JOptionPane.showConfirmDialog(null, "出現意外錯誤!", "系統提示", 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, "出現意外錯誤!", "系統提示", 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) {  /** 拖動過程的虛線選取框需自己實現 */  } }); 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; }}

希望本文所述對大家的java程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.