Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9031123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:39:09+00:00 2026-06-16T07:39:09+00:00

package demo; import java.awt.*; import java.awt.image.*; import java.util.*; import javax.swing.*; public class ScreenCapturingThread extends

  • 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;
}

Explanation

I have designed thread to go along with my screen recorder but I decided to test it first. This is what it is supposed to do:

  • create a new thread object by passing the appropriate parameters
  • take only one screen shot (during this test only), store it in the vector and before the run() ends, draw this on the JFrame so I can see what has been captured.
  • Problem

    I keep getting a NullPointerException at

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

    I don’t know what is going wrong.
    If you could please find out the bug?

    Update:

    enter image description here
    Now although the NullPointerException is gone, the frame is blank while it is not supposed to be

    Why?

    • 1 1 Answer
    • 0 Views
    • 0 Followers
    • 0
    Share
    • Facebook
    • Report

    Leave an answer
    Cancel reply

    You must login to add an answer.

    Forgot Password?

    Need An Account, Sign Up Here

    1 Answer

    • Voted
    • Oldest
    • Recent
    • Random
    1. Editorial Team
      Editorial Team
      2026-06-16T07:39:10+00:00Added an answer on June 16, 2026 at 7:39 am

      JFrame will not provide you with any Graphics until it is shown.

      If you want to draw before JFrame is shown you never should do like this:

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

      because as Andrew Thompson has correctly written:

      Do not use Component.getGraphics(). Instead, subclass and override the paint() (AWT), or paintComponent() (Swing) method.

      Component.getGraphics() simply can’t work. Java uses a callback mechanism for drawing graphics. You are not supposed to "push" graphics information into a component using getGraphics(). Instead you are supposed to wait until Java calls your paint()/paintComponent() method. At that moment you are supposed to provide the Component with the drawings you would like to do.

      instead you’ll be better off doing something like below:

      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);
      
      • 0
      • Reply
      • Share
        Share
        • Share on Facebook
        • Share on Twitter
        • Share on LinkedIn
        • Share on WhatsApp
        • Report

    Sidebar

    Related Questions

    package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
    package GC; import java.util.Scanner; public class main { public static void main(String args[]) {
    package cen.col.course.demo; import java.io.Serializable; public class Course implements Serializable { private static final long
    package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static
    package { import mx.controls.LinkButton; import flash.text.TextLineMetrics; public class multiLineLinkButton extends LinkButton { override protected
    Let's look at the following code snippet in Java. package demo; import java.util.Calendar; final
    package mp1practice; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level;
    I have written following code to get location name package demo.gps.locname; import java.io.IOException; import
    I'm trying out the LoaderCursor example in the API Demo: package com.example.android.apis.app; import android.app.Activity;
    package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =

    Explore

    • Home
    • Add group
    • Groups page
    • Communities
    • Questions
      • New Questions
      • Trending Questions
      • Must read Questions
      • Hot Questions
    • Polls
    • Tags
    • Badges
    • Users
    • Help
    • SEARCH

    Footer

    © 2021 The Archive Base. All Rights Reserved
    With Love by The Archive Base

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.