Problem With Image Data And getImage(...) Function

Hi Experts,

Whether you open accidently say a .txt file in Image data using getImage(..) or whatever the case is this method always and immediately returns non-null. Even if the image is not FULLY AND SUCCESSFULLY loaded but because of this function immediate returning of non-null value, you can't trace whether there is really image data or whether the image is successfully and fully loaded or not.

I have also use mediatracker with th images after calling getImage(..), and used the mediatracker waitforAll() methods but many times mostly when number of images are more, it too doesn't give the desired results.

Is there any way that the control of the java-applet program doesn't go any further unless an until the Image gets fully and successfully loaded.

In my applet there are 15-20 jpeg's (not big), but always two or three are left out giving a bad view of the applet to the end user.

Best Regards

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

> Is there any way that the control of the java-applet program

I've NEVER HAD ANY problems with MediaTracker. please post your code.
There are 3 files: class, java, htm. You will find detailed comments in the source file. When you use MediaTracker, you must keep in mind that waitForAll()
return when image successfully loaded, aborted or errored. You must use method such isErrorAny() to know more about loading status.

I've got the source file below -- if you'd like I can mail you copies of the other files.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class ImgApplet extends Applet implements Runnable {
 /**Image array*/
 private Image[] images;
 /**Information text*/
 private String info="";
 /**Loading complete flag*/
 private boolean isLoaded=false;
 /**Current image number*/
 private int imgNum=0;

 public void init() {
  images=new Image[15];//Create array of images
  //Start loading thread
  Thread t=new Thread(this);

 public void run() {
  //Create media tracker for tracking loading process
  MediaTracker tracker=new MediaTracker(this);
  boolean errFlg;//Error flag
  //Start loading loop
  for (int i=0;i<15;i++) {
   //Show number of loading image
   info="Loading image #"+(i+1)+"...";
   errFlg=false;//Reset error flag
   do {//Image load loop
    if (errFlg) {//If it second try or more show corresponding message
     info="Error loading image #"+(i+1)+", load again...";
    images[i]=getImage(getCodeBase(),"Images/Image"+(i+1)+".jpg");//Create image
    tracker.addImage(images[i],0);//Add image to tracker
    try {
     tracker.waitForAll();//Wait for try end
    } catch (InterruptedException e) {}
    errFlg=tracker.isErrorAny();//Check is image loaded correctly
    tracker.removeImage(images[i]);//Remove image from tracker
   } while (errFlg);//Image load loop execute until image are loaded correctly
   Thread.yield();//Pause thread and allow other threads to execute
  //Stop loading loop and show message and image to screen
  info="All images successfully loaded.";
  enableEvents(AWTEvent.MOUSE_EVENT_MASK);//Enable mouse events

 protected void processMouseEvent(MouseEvent e) {
  //This method provide ability to view images
  if (e.getID()==MouseEvent.MOUSE_PRESSED) {
   if ((e.getModifiers() & InputEvent.BUTTON1_MASK)!=0) {
    if (imgNum>0) {
   else {
    if (imgNum<14) {

 public void paint(Graphics g) {
  if (isLoaded) {
   g.drawString("Press left mouse button for previous image, right for next.",10,40);


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
austin123Author Commented:
Thanks heyhey and bpouydog,

I will explain you things now after 4 days of long work. I too believed that mediatracker cannot work in a wrong way as you also quoted heyhey, But still the following problem is occuring with my code.

I have 8 buttons on my applet and each button has image(gif) over it, my code all in the init() method::

..... for all eight gif's, then

....... for all eight,then

}catch(InterruptedException e)
if (mediatracker1.isErrorAny()==true)
System.out.println("Yes Errors");
System.out.println("No Errors");

Console Prints that there are No Errors, after which I create buttons as ::

JButoon button1=new JButton(button1gif)
..... and so on for the rest buttons

But the gif's can be seen only in the last four buttons and not the first four. No doubt, apart from these images there are some more images which gets downloaded for my applet to work. But for all I have used the same technique ..

waitforall() and IsErrorAny(), and everywhere I gets "No ERROR". Rest all images are fine. The problem is with the gif's of the FIRST FOUR BUTTONS. ALSO THE PROBLEM IS NOT ALWAYS. FEW TIMES THEY ALL GETS DOWNLOADED say 1 out of 4.

I don't know why this is happening, Please see to it.

Best Regards

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

can you post the whole code and preferebly the compiled applet on some URL that we can access ?
Asta CuTechnical consultant & graphic designCommented:

This question is still open today.  Perhaps it was overlooked.  If you've been helped, just choose the expert's comment which served your needs as the accepted answer to then grade and close.  If more is needed, please advise.

Your responsiveness is appreciated.

austin123---->  You have asked 30 question at this site and only graded 19, this is unacceptable.

Please check the Help Desk link on the left about site-related guidelines and the Member Agreement.

I will update all your open questions and monitor them for closure.  Please remain active in all your open questions, provide feedback to the expert who help you and when the process is complete, award the expert(s) with points.  If you need help in this regard, post a zero point question in Community Support topic area with question link and request.

Experts, please let me know your recommendations as to the fair outcome of this question if Asker chooses not to finalize this.


Community Support Moderator @ Experts Exchange
Expert input is welcome here, since it is clear that austin123 is not returning to finalize open questions as to a fair outcome and to ensure the quality we want enters the PAQ.


Community Support Moderator @ Experts Exchange
Austin has indicated in another question that the intention is to return and update all open questions.  

Thanks for your patience, all.

Community Support Moderator @ Experts Exchange
Please let the experts know here about the results of their recommendations made to you.  If more than one helped you, I can split the points to those who did.  Just comment here with details.


Community Support Moderator @ Experts Exchange
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.