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
消失了,但框架是空白的,而它不应该是