毅两小面2010-07-23TA获得超过130个赞关注找茬j**a代码importj**a.awt.*;importj**a.awt.event.ActionEvent;importj**a.awt.event.ActionListener;importj**a.awt.event.KeyEvent;importj**a.awt.event.WindowAdapter;importj**a.awt.event.WindowEvent;importj**ax.swing.JButton;importj**ax.swing.JComponent;importj**ax.swing.JFrame;importj**ax.swing.KeyStroke;classImageCanva**tendsCanvas{/****/privatestaticfinallongserialVersionUID=-2094477381599040916L;privateImageimage;privateDimensionprefSize;publicImageCanvas(Imageimage){this.image=image;calculatePreferredSize();}publicvoidsetImage(Imageimage){this.image=image;calculatePreferredSize();repaint();}privatevoidcalculatePreferredSize(){prefSize=newDimension(image.getWidth(this),image.getHeight(this));System.out.println(image.getWidth(this));setSize(prefSize);}publicDimensiongetPreferredSize(){returnprefSize;}publicDimensiongetMinimumSize(){returnprefSize;}publicvoi**pdate(Graphicsg){paint(g);}publicvoidpaint(Graphicsg){g.drawImage(image,0,0,**ll);}}classImageUtils{publicstaticImagegetScreenImage(){Robotrobot;try{robot=newRobot();}catch(Exceptione){thrownewRuntimeException("unabletoconstrUCtRobot");}//DimensionscreenDims=Toolkit.getDefaultToolkit().getScreenSize();Imagescreen=robot.createScreenCapture(newRectangle(644,202,498,447)).getScaledInstance(498,447,Image.SCALE_SMOOTH);MediaTrackertracker=newMediaTracker(newLabel());//媒体追踪器tracker.addImage(screen,1);try{tracker.waitForID(0);}catch(InterruptedExceptione){/***/}returnscreen;}}classKeyDemo1extendsJFrame{/****/privatestaticfinallongserialVersionUID=3862477931331240418L;JButtonbutton;publicKeyDemo1(){addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});buildFrame();setLocation(250,150);setSize(200,100);setVisible(true);}privatevoidbuildFrame(){KeyStrokeks;Containerpane=getContentPane();button=newJButton("Focus");pane.add(button);pane.addNotify();//setAlwaysOnTop(true);button.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Stringcommand=e.getActionCommand();if(command=="Focus"){if(!(ScreenCapture.qq))ScreenCapture.qq=true;else{ScreenCapture.qq=false;ScreenCapture.dd=true;}}}});}}publicclassScreenCaptureextendsFrame{/****/privatestaticfinallongserialVersionUID=2836175805057121926L;staticbooleancc,dd=false;publicstaticbooleanqq=false;publicvoidwindowClosing(WindowEvente){System.exit(0);}publicstaticImageCanvascanvas=newImageCanvas(ImageUtils.getScreenImage());publicScreenCapture(){add(canvas);setLocation(135,202);setSize(498,447);setUndecorated(true);//设置无边框this.setAlwaysOnTop(true);//设置总在最前setVisible(cc);ThreadimageThread=newUpdateThread();imageThread.setDaemon(true);//将该线程标记为守护线程或用户线程。imageThread.start();}classUpdateThreadextendsThread{publicvoidrun(){while(true){try{Thread.sleep(83);if(qq){if(!cc){setVisible(true);cc=true;}else{setVisible(false);cc=false;}}else{System.out.println("没打开窗口");if(dd)setVisible(false);dd=false;}}catch(Exceptionex){}canvas.setImage(ImageUtils.getScreenImage());validate();//验此容器及其所有子组件。}}}publicstaticvoidmain(String[]args){newScreenCapture();newKeyDemo1();}}