<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 38,  comments - 22,  trackbacks - 0

    桌面截圖功能

    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;
    ?}
    }

    截屏

    public class GuiCamera {
    ?private String fileName; //文件的前綴

    ?private String defaultName = "GuiCamera";

    ?static int serialNum = 0;

    ?private String imageFormat; //圖像文件的格式

    ?private String defaultImageFormat = "png";

    ?Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    ?/****************************************************************
    ? * 默認的文件前綴為GuiCamera,文件格式為PNG格式
    ? * The default construct will use the default?
    ? * Image file surname "GuiCamera",?
    ? * and default image format "png"
    ? ****************************************************************/
    ?public GuiCamera() {
    ??fileName = defaultName;
    ??imageFormat = defaultImageFormat;

    ?}

    ?/****************************************************************
    ? * @param s the surname of the snapshot file
    ? * @param format the format of the? image file,?
    ? * it can be "jpg" or "png"
    ? * 本構造支持JPG和PNG文件的存儲
    ? ****************************************************************/
    ?public GuiCamera(String s, String format) {

    ??fileName = s;
    ??imageFormat = format;
    ?}

    ?/****************************************************************
    ? * 對屏幕進行拍照
    ? * snapShot the Gui once
    ? ****************************************************************/
    ?public void snapShot() {

    ??try {
    ???//拷貝屏幕到一個BufferedImage對象screenshot
    ???BufferedImage screenshot = (new Robot())
    ?????.createScreenCapture(new Rectangle(0, 0,
    ???????(int) d.getWidth(), (int) d.getHeight()));
    ???serialNum++;
    ???//根據文件前綴變量和文件格式變量,自動生成文件名
    ???String name = fileName + String.valueOf(serialNum) + "."
    ?????+ imageFormat;
    ???File f = new File(name);
    ???System.out.print("Save File " + name);
    ???//將screenshot對象寫入圖像文件
    ???ImageIO.write(screenshot, imageFormat, f);
    ???System.out.print("..Finished!\n");
    ??} catch (Exception ex) {
    ???System.out.println(ex);
    ??}
    ?}

    ?public static void main(String[] args) {
    ??GuiCamera cam = new GuiCamera("d:\\Hello", "png");//

    ??cam.snapShot();
    ?}
    }

    透明窗體
    public class TransparentBackground extends JComponent implements
    ??ComponentListener, WindowFocusListener, Runnable {
    ?private JFrame frame;

    ?private Image background;

    ?private long lastupdate = 0;

    ?public boolean refreshRequested = true;

    ?public TransparentBackground(JFrame frame) {
    ??this.frame = frame;
    ??updateBackground();
    ??frame.addComponentListener(this);
    ??frame.addWindowFocusListener(this);
    ??new Thread(this).start();
    ?}

    ?public void componentShown(ComponentEvent evt) {
    ??repaint();
    ?}

    ?public void componentResized(ComponentEvent evt) {
    ??repaint();
    ?}

    ?public void componentMoved(ComponentEvent evt) {
    ??repaint();
    ?}

    ?public void componentHidden(ComponentEvent evt) {
    ?}

    ?public void windowGainedFocus(WindowEvent evt) {
    ??refresh();
    ?}

    ?public void windowLostFocus(WindowEvent evt) {
    ?? refresh();
    ?}

    ?public void refresh() {
    ??if (frame.isVisible()) {
    ???repaint();
    ???refreshRequested = true;
    ???lastupdate = new Date().getTime();
    ??}
    ?}

    ?public void run() {
    ??try {
    ???while (true) {
    ????Thread.sleep(250);
    ????long now = new Date().getTime();
    ????if (refreshRequested && ((now - lastupdate) > 1000)) {
    ?????if (frame.isVisible()) {
    ??????Point location = frame.getLocation();
    //??????frame.setVisible(false);
    ??????updateBackground();
    //??????frame.setVisible(true);
    ??????frame.setLocation(location);
    ??????refresh();
    ?????}
    ?????lastupdate = now;
    ?????refreshRequested = false;
    ????}
    ???}
    ??} catch (Exception ex) {
    ???ex.printStackTrace();
    ??}
    ?}

    ?public static void main(String[] args) {
    ??JFrame frame = new JFrame("Transparent Window");
    ??frame.setUndecorated(true);

    ??TransparentBackground bg = new TransparentBackground(frame);
    ??// bg.snapBackground();
    ??bg.setLayout(new BorderLayout());

    ??JPanel panel = new JPanel() {
    ???public void paintComponent(Graphics g) {
    ????g.setColor(Color.blue);
    ????Image img = new ImageIcon("d:/moon.gif").getImage();
    ????g.drawImage(img, 150, 150, null);
    ???//?g.drawLine(0,0, 600, 500);
    ???}
    ??};
    ??panel.setOpaque(false);
    ??JLabel jl=new JLabel(new ImageIcon("d:/moon.gif"));
    ??
    ??
    ??bg.add("North", jl);
    ??bg.add("Center", panel);

    ??frame.getContentPane().add("Center", bg);
    ??frame.pack();
    ??Toolkit tk = Toolkit.getDefaultToolkit();
    ??Dimension dim = tk.getScreenSize();
    ??frame.setSize((int) dim.getWidth(), (int) dim.getHeight());
    ??frame.setLocationRelativeTo(null);
    ??frame.show();
    ?}

    ?public void updateBackground() {
    ??try {
    ???Robot rbt = new Robot();
    ???Toolkit tk = Toolkit.getDefaultToolkit();
    ???Dimension dim = tk.getScreenSize();
    ???background = rbt.createScreenCapture(new Rectangle(0, 0, (int) dim
    ?????.getWidth(), (int) dim.getHeight()));
    ??} catch (Exception ex) {
    ???// p(ex.toString( ));
    ???// 此方法沒有申明過,因為無法得知上下文。因為不影響執行效果,先注釋掉它
    ???ex.printStackTrace();
    ??}
    ?}

    ?public void paintComponent(Graphics g) {
    ??Point pos = this.getLocationOnScreen();
    ??Point offset = new Point(-pos.x, -pos.y);
    ??g.drawImage(background, offset.x, offset.y, null);
    ?}
    }

    posted on 2007-01-15 14:29 aaabbb 閱讀(373) 評論(0)  編輯  收藏 所屬分類: Swing
    主站蜘蛛池模板: 亚洲AⅤ男人的天堂在线观看| 亚洲人成在线中文字幕| 真人无码作爱免费视频| 成人看的午夜免费毛片| 亚洲 日韩 色 图网站| 最近最新的免费中文字幕| 亚洲最大天堂无码精品区| 国产一精品一AV一免费孕妇| 最新国产成人亚洲精品影院| 最近中文字幕mv免费高清电影 | 亚洲中文字幕无码不卡电影| 国产人成网在线播放VA免费| 国产精品亚洲а∨无码播放| 成人无码a级毛片免费| 亚洲视频精品在线| 野花高清在线观看免费完整版中文| 香蕉大伊亚洲人在线观看| 两个人的视频高清在线观看免费| 亚洲欧美国产国产综合一区| avtt亚洲天堂| 久别的草原电视剧免费观看| 亚洲白色白色在线播放| 免费看香港一级毛片| WWW国产成人免费观看视频| 亚洲日韩精品无码专区网址| 日本片免费观看一区二区| 亚洲国产精品99久久久久久| 亚洲精品综合久久| 精品一区二区三区免费毛片爱| 亚洲xxxxxx| 77777亚洲午夜久久多人| 中文字幕成人免费视频| 亚洲精品无码永久在线观看男男| 亚洲av中文无码| 国产免费av一区二区三区| 手机永久免费的AV在线电影网| 久久精品7亚洲午夜a| 午夜毛片不卡免费观看视频| 精品国产污污免费网站入口| 亚洲AV无码久久久久网站蜜桃| 亚洲国产精品成人久久蜜臀 |