Results 1 to 4 of 4

Thread: checking for Image loaded

  1. #1

    Thread Starter
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144

    checking for Image loaded

    I'm making a card game and loading a load of images for all the cards using this code...

    Code:
    for (int i=0; i<52; i++)
    {
    	URL url = this.getClass().getResource("Resources/" + theDeck.getCard(i).getFilename() + ".jpg");
    	cardImages[i] = this.getImage(url);
    }
    getImage returns straight away regardless of wether its finished loading the image or not. I was wondering if anyone knew how to check if the Image has finished loading or anyway of getting it to return once it has loaded.

    Thanks alot
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

  2. #2
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    I can help ya with this... one possibility is to use the MediaTracker class. Here is an short example, but you should check out the MediaTracker class in the doc.

    When loading an image over the net you frequently have to allow
    for long load times. The MediaTracker class is used to monitor a
    resource so that other things can happen (ie. threads) while the
    image is loading. However, sometimes you really need for the
    image to totally load because you need the height and width
    measurements before continuing. The following code fragment
    show how to wait for an image to finish loading.

    // url is a String containing the URL of the image to load.
    MediaTracker tr;
    Image im = toolkit.getImage(url);

    tr.addImage(im, 0);
    tr.waitForID(0);

    // at this point the image is finished
    // loading and the height and width can
    // be determined.
    int w = im.getWidth(this);
    int h = im.getHeight(this);

    Hope this helps
    Bill

  3. #3
    Hyperactive Member
    Join Date
    May 2000
    Posts
    367
    I dont remember but I think Media tracker has a wait for all has well, so I would use that in your case. Also I am not sure where you are loading the images, but I would load all the images you need in the beginning that way you have them all at your disposal during runtime which is worth the little bit of extra wait in the initialization.

  4. #4

    Thread Starter
    Addicted Member Mrs Kensington's Avatar
    Join Date
    Sep 2001
    Location
    Dorset, UK
    Posts
    144
    thanks that looks perfect! I'll have a play with it this afternoon!
    Oh and yes I am loading all the images up at the beginning!

    Mrs K
    Ford? Theres an infinite number of monkeys outside that want to talk to you about a script of hamlet they've produced!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width