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

  • Home
  • SEARCH
  • 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 824329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:05:10+00:00 2026-05-15T03:05:10+00:00

I have a thread in my screen recording application that won’t cooperate: package recorder;

  • 0

I have a thread in my screen recording application that won’t cooperate:

package recorder;

import java.awt.AWTException;
import java.awt.Insets;
import java.io.IOException;

import javax.swing.JFrame;

public class RepeatThread extends Thread {
    volatile boolean stop;
    public volatile Thread recordingThread;
    JFrame frame;
    int count = 0;

    RepeatThread( JFrame myFrame ) {
        stop = false;
        frame = myFrame;
    }

    public void run() {
        while( stop == false ) {
            int loopDelay = 33; // 33 is approx. 1000/30, or 30 fps
            long loopStartTime = System.currentTimeMillis();
            Insets insets = frame.getInsets(); // Get the shape we're recording

            try {
                ScreenRecorder.capture( frame.getX() + insets.left, 
                        frame.getY() + insets.top, frame.getWidth()
                        - ( insets.left + insets.right ), 
                        frame.getHeight() - ( insets.top + insets.bottom ) );
            }
            catch( AWTException e1 ) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            catch( IOException e1 ) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } // Add another picture

            long loopEndTime = System.currentTimeMillis();
            int loopTime = (int )( loopEndTime - loopStartTime );
            if( loopTime < loopDelay ) {
                try {
                    sleep( loopDelay - loopTime ); // If we have extra time,
                                                   // sleep
                }
                catch( Exception e ) {
                } // If something interrupts it, I don't give a crap; just
                  // ignore it
            }
        }

    }

    public void endThread() {
        stop = true;
        count = 0;
        ScreenRecorder.reset();
        // Once I get this annoying thread to work, I have to make the pictures
        // into a video here!
    }
}

It’s been bugging me for ages. It periodically takes screenshots to the specified area.

When you start recording, it hides (decativates) the window. On a Mac, when you give an application focus, any hidden windows will activate. In my class WListener (which I have confirmed to work), I have:

    public void windowActivated(WindowEvent e) {
        if(ScreenRecorder.recordingThread != null) {
            ScreenRecorder.recordingThread.endThread();
        }
    }

So what SHOULD happen is, the screenshot-taking thread stops when he clicks on the application. However, I must be brutally screwing something up, because when the thread is running, it won’t even let the window reappear. This is my first thread, so I expected a weird problem like this. Do you know what’s wrong?

EDIT: Okay, I made stop volatile, and here is the place where I make the thread:

package recorder;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

public class ScreenRecorder {

    static RepeatThread recordingThread;
    static int count;

    public static void record(JFrame frame) {

        if(recordingThread == null) { //Make a new thread if we don't have one
            recordingThread = new RepeatThread(frame);
            recordingThread.start();
        }
    }

    public static void capture(int x, int y, int width, int height) throws AWTException, IOException {
        // capture the whole screen
        //BufferedImage screencapture = new Robot().createScreenCapture(
        //      new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) );

        BufferedImage screencapture = new Robot().createScreenCapture(
                new Rectangle( x, y, width, height));

        // Save as JPEG
        File directory = new File("/Users/stuart/Movies/temp");
        if(directory.exists() == false) {
            directory.mkdirs();
        }

        count ++;
        File file = new File("/Users/stuart/Movies/temp/screencapture" + count + ".jpg");
        ImageIO.write(screencapture, "jpg", file);


        // Save as PNG
        // File file = new File("screencapture.png");
        // ImageIO.write(screencapture, "png", file);

    }

    public static void stop() {

    }

    public static void reset() {
        count = 0;
    }
}
  • 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-05-15T03:05:11+00:00Added an answer on May 15, 2026 at 3:05 am

    Since you are clearly trying to execute a unit of work every X milliseconds, this will be far far easier if you use Java’s Executors:

    ScheduledExecutorService service = executors.newSingleThreadScheduledExecutor();
    service.scheduleAtFixedRate(new Runnable() {
      public void run() {
        // do one iteration of your work
        ScreenRecorder.capture(...);
        ...
      }
    }, 0L, 33L, TimeUnit.MILLISECONDS);
    
    ...
    service.shutdown(); // to stop
    

    Doing this manually with Thread isn’t nearly as messy as you’re making it (not a dig at you, just saying it’s not that terrible in Java), but the above is still by far the simplest option.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one thread that writes results into a Queue. In another thread (GUI),
I have a thread that, when its function exits its loop (the exit is
I have a thread that needs to be executed every 10 seconds. This thread
In my Silverlight project I have a thread that fires every x milliseconds. In
I've seen a number of examples that have a thread procedure that looks like
In my C# program, I have a thread that represents a running test, which
I have thread exception handler which saves the exception stack trace and should close
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have a thread push-backing to STL list and another thread pop-fronting from the
I need to have a thread signal another if the user wishes to interrupt

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.