在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

用Java模拟出QQ桌面截图功能

2016-12-20 13:15| 发布者: zhangjf| 查看: 447| 评论: 0

摘要: Q的桌面截图功能非常方便,去年曾用Java模拟过一个,现整理出来。 本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示,这样就模拟出了一个桌面,Java也就可以获得鼠标的作用区域从 ...
Q的桌面截图功能非常方便,去年曾用Java模拟过一个,现整理出来。
本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示,这样就模拟出了一个桌面,Java也就可以获得鼠标的作用区域从而实现桌面中的小范围截屏。
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.awt.event.MouseAdapter;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseMotionListener;
  8. /**
  9. * 用Java模拟出QQ桌面截图功能
  10. * @author 五斗米 <如转载请保留作者和出处>
  11. * @blog <a href="http://blog.csdn.net/mq612">http://blog.csdn.net/mq612
  12. */
  13. public class Test extends JFrame {
  14. private static final long serialVersionUID = -267804510087895906L;
  15. private JButton button = null;
  16. private JLabel imgLabel = null;
  17. public Test() {
  18. button = new JButton("模拟屏幕(点右键退出)");
  19. button.addActionListener(new ActionListener() {
  20. public void actionPerformed(ActionEvent e) {
  21. try {
  22. new ScreenWindow(imgLabel);
  23. } catch (Exception e1) {
  24. JOptionPane.showConfirmDialog(null, "出现意外错误!", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
  25. }
  26. }
  27. });
  28. JPanel pane = new JPanel();
  29. pane.setBackground(Color.WHITE);
  30. imgLabel = new JLabel();
  31. pane.add(imgLabel);
  32. JScrollPane spane = new JScrollPane(pane);
  33. this.getContentPane().add(button, BorderLayout.NORTH);
  34. this.getContentPane().add(spane);
  35. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  36. this.setSize(300, 200);
  37. this.setLocationRelativeTo(null);
  38. this.setVisible(true);
  39. }
  40. public static void main(String[] args) {
  41. new Test();
  42. }
  43. }
  44. class ScreenWindow extends JFrame {
  45. private static final long serialVersionUID = -3758062802950480258L;
  46. private boolean isDrag = false;
  47. private int x = 0;
  48. private int y = 0;
  49. private int xEnd = 0;
  50. private int yEnd = 0;
  51. public ScreenWindow(final JLabel imgLabel) throws AWTException, InterruptedException {
  52. Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
  53. JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0, 0, screenDims.width, screenDims.height)));
  54. label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
  55. label.addMouseListener(new MouseAdapter() {
  56. public void mouseClicked(MouseEvent e) {
  57. if (e.getButton() == MouseEvent.BUTTON3) {
  58. dispose();
  59. }
  60. }
  61. public void mousePressed(MouseEvent e) {
  62. x = e.getX();
  63. y = e.getY();
  64. }
  65. public void mouseReleased(MouseEvent e) {
  66. if (isDrag) {
  67. xEnd = e.getX();
  68. yEnd = e.getY();
  69. if(x > xEnd){
  70. int temp = x;
  71. x = xEnd;
  72. xEnd = temp;
  73. }
  74. if(y > yEnd){
  75. int temp = y;
  76. y = yEnd;
  77. yEnd = temp;
  78. }
  79. try {
  80. imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x, y, xEnd - x, yEnd - y)));
  81. } catch (Exception ex) {
  82. JOptionPane.showConfirmDialog(null, "出现意外错误!", "系统提示", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
  83. }
  84. dispose();
  85. }
  86. }
  87. });
  88. label.addMouseMotionListener(new MouseMotionListener() {
  89. public void mouseDragged(MouseEvent e) {
  90. if(!isDrag)
  91. isDrag = true;
  92. }
  93. public void mouseMoved(MouseEvent e) {
  94. /** 拖动过程的虚线选取框需自己实现 */
  95. }
  96. });
  97. this.setUndecorated(true);
  98. this.getContentPane().add(label);
  99. this.setSize(screenDims.width, screenDims.height);
  100. this.setVisible(true);
  101. this.setExtendedState(JFrame.MAXIMIZED_BOTH);
  102. }
  103. }
  104. class ScreenImage {
  105. public static Image getScreenImage(int x, int y, int w, int h) throws AWTException, InterruptedException {
  106. Robot robot = new Robot();
  107. Image screen = robot.createScreenCapture(new Rectangle(x, y, w, h)).getScaledInstance(w, h, Image.SCALE_SMOOTH);
  108. MediaTracker tracker = new MediaTracker(new Label());
  109. tracker.addImage(screen, 1);
  110. tracker.waitForID(0);
  111. return screen;
  112. }
  113. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-23 04:29

Copyright 2015-2025 djqfx

返回顶部