mt object wouldnt load image

Hello,

Here is the code for MediaTracker object it doesnt loads image, the image is located in package DrawImage, also what is the difference between getDocumentBase() and getCodeBase()

   package DrawImage;
     
    import java.net.*;     
    import java.awt.*;
    
    public class LoadImage extends java.applet.Applet 
    {
        Image uc  ;
        URL base;
        MediaTracker mt;       
    
    	public void init()
    	{
       		mt = new MediaTracker(this);
        	try{
            		base = getDocumentBase();
            		}catch(Exception e){}
        
        	uc = getImage(base, "src//DrawImage//java.gif");
	        mt.addImage(uc,1);
        
        	try{
	        mt.waitForAll();
        	}catch(InterruptedException e){}    
	}
    
    	public void paint(Graphics g)
	{   
        	g.drawImage(uc, 45  , 45, Color.yellow, this);
    	}        
   }   

MediaTracker is a very outdated way of loading images. I strongly recommend using ImageIO.read() instead!

thanks, here is imageio but this also doesnt loads the image

package DrawImage;

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
 
public class LoadImage extends Applet {
 
     private BufferedImage img;
 
     public void init() {
         try {
             URL url = new URL(getCodeBase(), "src//DrawImage//java.GIF");
             img = ImageIO.read(url);
         } catch (IOException e) {
         }
     }
 
     public void paint(Graphics g) {
       g.drawImage(img, 50, 50, null);
     }
}

You should never ever silently swallow an exception like that. You’re probably getting an error telling you what the problem is.

package DrawImage;

import java.applet.Applet;
import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.*;
 
public class LoadWindows extends Applet {
 
     private BufferedImage img;
 
     public void init() {
         try {
             URL url = new URL(getCodeBase(), "src//DrawImage//java.GIF");
             img = ImageIO.read(url);
         } catch (IOException e) {
             System.out.println(e);
         }
     }
 
     public void paint(Graphics g) {
       g.drawImage(img, 50, 50, null);
     }
}

here is the code with exception and it says:“javax.imageio.IIOException: Can’t get input stream from URL!”

Not sure why you think you have to use “//” – did you see “\” and attempt to swap the slashes to make it cross-platform? Simply use “/”

That will be the first step to solve your problem. This thread will help you with the next steps:

thanks works now with the toolkit

package DrawImage;   

import java.applet.Applet;
import java.awt.*;

public class Load extends Applet
{
   private Image img;   

   public void init()
   {
      img = null;
      
   }
   public void loadImage()
   {
      try
      { 
         img = Toolkit.getDefaultToolkit().getImage("src//DrawImage//java.gif");;
      }
      catch(Exception e) { }
   }
   public void paint(Graphics g)
   {
      if (img == null)
         loadImage();
      g.drawImage(img, 0, 0, this);
   }
}