0
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);
    

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

    更新:

    在此处输入图像描述 现在虽然NullPointerException消失了,但框架是空白的,而它不应该是

    为什么?

    4

    1 回答 1

    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 所写的那样:

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

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

    相反,您最好执行以下操作:

    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);
    
    于 2012-12-23T20:54:59.473 回答