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 4096864
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:06:02+00:00 2026-05-20T20:06:02+00:00

Loading the image one time works as done in the following steps (for code

  • 0

Loading the image one time works as done in the following steps (for code see end of post):

  1. load the URI
  2. with the Toolkit load the image from the URI
  3. use a MediaTracker to check the loading(waitForAll())
  4. change the viewports size according to the image
  5. (repaint() tried here, didn’t change anything)

The resizing will be done in “no time” and after that it takes a second or two that the image actually is shown (maybe because of my slow computer).
If I put a while(true) around that without letting him wait for some time, he will load like crazy and probably never take the time to show anything (on all computers not just mine). So I want him to wait. There is not much happening on the webcam, so every 10 seconds might be a nice interval.

The two parts of my question are:

  1. How do I let the loop wait in a smart way? Inside it’s own thread? Just putting a Thread.wait(10000); at the bottom of the loop will also stop the picture from being displayed.

  2. How can I check if the picture is displayed and not just loaded like the MediaTracker does?

I know that I actually don’t ask how to solve a problem but how to implement a solution, which might be the wrong one in the first place. For that case I add the context, too: I want to display Images from a webcam that I can get through an URL as a JPG which will be rewritten on the server in an unknown interval. This connection to the Webcam might be smart or stupid, but I can’t change it anyway. Not my server. I just want to write a client to show the pictures.

The code for reference and Great Justice (inside an actionPerformed()):

...
    // now use an URI even if Toolkit.getImage() uses URL, as
// recommended here:
// http://download.oracle.com/javase/6/docs/api/java/net/URL.html
URI adress = null;
try {
    adress = new URI("http://194.95.48.67/record/current.jpg");
} catch (URISyntaxException e1) {
    throw new Error(e1);
}
displaystate = STATE_LOADING;
repaint();
System.out.println("load image from: " + adress);
try {
    image = getToolkit().getImage(adress.toURL());
} catch (MalformedURLException e1) {
    throw new Error(e1);
}
MediaTracker mt = new MediaTracker(this);
mt.addImage(image, 0);
try {
    mt.waitForAll();
} catch (InterruptedException e) {
    // nothing
}
filename = adress.toString();
imagesize.width = image.getWidth(null);
imagesize.height = image.getHeight(null);
if (imagesize.width == -1 || imagesize.height == -1) {
    displaystate = STATE_LOADERROR;
} else {
    viewport.x = 0;
    viewport.y = 0;
    viewport.width = imagesize.width;
    viewport.height = imagesize.height;
    setSize(viewport.width, viewport.height);
    displaystate = STATE_DISPLAYING;
}
  • 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-20T20:06:03+00:00Added an answer on May 20, 2026 at 8:06 pm

    If you call Thread.sleep the current thread can’t do anything meanwhile. So if you do this from the main thread (as I assume, as you question if a second thread should be used), the GUI cannot update and thus not repaint the image. This is why it hangs if you put the sleep at the end of the loop.

    Second thing is that repaint doesn’t repaint immediately. It simply posts an event in the AWT event loop that will be processed eventually, and only then the component is repainted.

    The simplest way to do it is like this:
    Make a second thread that loads the image, and then stores in in a member variable. After that it calls repaint, and then sleeps.
    In the paint method you take the image and draw it on screen. This will happen in the main GUI thread, so the other thread’s sleep won’t harm.

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

Sidebar

Related Questions

I was loading an image resource with the following code and it works fine
In Prototype I can show a loading... image with this code: var myAjax =
Once the page has finished loading, I would like one image to cross-fade to
I just wrote a js code for a slideshow, using only one image (sprite).
I keep running across this loading image http://georgia.ubuntuforums.com/images/misc/lightbox_progress.gif which seems to have entered into
I'm trying to display an update progress loading image whenever my update panel does
I'm loading an image from a file, and I want to know how to
I was loading a Bitmap Image from a File. When I tried to save
When loading a page for the first time (!IsPostback), I am creating a button
I would like to stop images from loading, as in not even get a

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.