/*以下代碼用于連續(xù)捕獲桌面,但是用setRGB()更新之后,再重繪窗口,沒有什么反應(yīng)。*/import java.awt.*;import java.awt.image.BufferedImage;public class Image extends Frame {static BufferedImage im;public static void main(String[] args){ int i;Image image=new Image(800,600);im=image.capSc();//獲取第一楨BufferedImage temp;for(i=0;i<1000;i++){temp=image.capSc();//捕獲桌面圖像image.getdata(temp);//與原圖像逐相素對比更新image.repaint();//重繪}}public void getdata(BufferedImage temp){int i,j,height,width,pix;width=temp.getWidth();height=temp.getHeight();for(i=0;i<width-1;i+=2)for(j=0;j<height-1;j+=2){ pix=temp.getRGB(i,j);if(pix!=im.getRGB(i,j))//每個相素進行比較如果不等{im.setRGB(i,j,pix);//則更新為現(xiàn)在的像素值。}}}public BufferedImage capSc()//這個函數(shù)可略過不看,捕獲桌面,并返回圖像{ BufferedImage image=null;try{Robot robot=new Robot();Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();Rectangle screenRectangle = new Rectangle(screenSize);image = robot.createScreenCapture(screenRectangle);return image;} catch(Exception exe){exe.printStackTrace();}return image;}public Image(int width,int height){setSize(width,height);setVisible(true);}public void paint(Graphics g){g.drawImage(im,0,0,800,600,this);}}
圖像始終是第一楨。為什么,這是為什么?
慕的地8271018
2022-10-12 15:11:47