创建一个线程来重复捕获屏幕

时间:2012-12-23 20:47:04

标签: java exception jframe bufferedimage

package demo;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.swing.*;

public class ScreenCapturingThread extends Thread{
    public ScreenCapturingThread(Vector<BufferedImage> screenShots,
            int frameRate,
            Icon cursor,
            Rectangle recordingArea){
        this.screenShots = screenShots;
        this.frameRate = frameRate;
        this.cursor = cursor;
        this.recordingArea = recordingArea;

        try{
            bot = new Robot();
        }catch(Exception e){
            System.out.println(e);
        }
        calculateSleepTime();
    }
    @Override
    public void run(){
        while(keepCapturing == true){
            try{
                screenShots.add(takeScreenShot());
                sleep(sleepTime);

                keepCapturing = false; //take only one shot

                System.out.println("here");
                JFrame frame = new JFrame();
                frame.setSize(recordingArea.width,recordingArea.height);
                frame.getGraphics().drawImage(screenShots.firstElement(), 0, 0,frame);
                frame.repaint();
                frame.setVisible(true);

            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }
    }
    public BufferedImage takeScreenShot(){
        p = m.getPointerInfo();
        Point location = p.getLocation();
        image = bot.createScreenCapture(recordingArea);
        if(cursor!=null){
            Graphics g = image.getGraphics();
            g.drawImage(((ImageIcon)cursor).getImage(), location.x,location.y,null);
        }

        return image;
    }
    public void stopIt(){
        keepCapturing = false;
    }
    public void calculateSleepTime(){
        sleepTime = 1/frameRate;
    }

    public static void main(String[] args) {
        Vector<BufferedImage> bufferedImages = new Vector<>(100);
        int frameRate = 10;
        Icon cursor = (Icon) new ImageIcon("src/images/blackCursor.png");
        Rectangle r = new Rectangle(1280,800);
        ScreenCapturingThread sc = new ScreenCapturingThread(bufferedImages,frameRate,cursor,r);

        sc.start();
    }
    Vector<BufferedImage> screenShots;
    int frameRate;
    long sleepTime;
    boolean keepCapturing = true;
    Icon cursor;
    Rectangle recordingArea;
    Robot bot;
    MouseInfo m;
    PointerInfo p;
    BufferedImage image;
}

解释

我设计的线程与我的屏幕录像机一起使用但我决定先测试它。这是应该做的:

  • 通过传递适当的参数
  • 来创建一个新的线程对象
  • 只使用一次屏幕截图(仅在此测试期间),将其存储在向量中,在run()结束之前,在JFrame上绘制此内容,以便我可以看到已捕获的内容。
  • 问题

    我一直在NullPointerException

    frame.getGraphics().drawImage(screenShots.firstElement(), 0, 0,frame);
    

    我不知道出了什么问题 如果可以请找出错误?

    更新:

    enter image description here 现在虽然NullPointerException已消失,但框架为空白,但不应该

    为什么?

    1 个答案:

    答案 0 :(得分:3)

    JFrame在显示之前不会向您提供任何Graphics

    如果你想在显示JFrame之前画画,永远不应该这样做:

    frame.pack();
    frame.setSize(recordingArea.width, recordingArea.height);
    Graphics g = frame.getContentPane().getGraphics();
    g.drawImage(screenShots.firstElement(), 0, 0, frame);
    

    因为Andrew Thompson has correctly written

      

    不要使用Component.getGraphics()。相反,子类化并覆盖paint()(AWT)或paintComponent()(Swing)方法。

         

    Component.getGraphics()根本无法工作。 Java使用回调机制绘制图形。您不应该使用getGraphics()将图形信息“推送”到组件中。相反,你应该等到Java调用你的paint()/ paintComponent()方法。那时你应该为Component提供你想要的图纸。

    相反,你最好做下面的事情:

    BufferedImage img = new BufferedImage(recordingArea.width, recordingArea.height,
                        BufferedImage.TYPE_3BYTE_BGR);
                Graphics g = img.createGraphics();
                g.drawImage(screenShots.firstElement(), 0, 0, frame);
                JLabel l = new JLabel(new ImageIcon(img));
                frame.getContentPane().add(l);