Help loading images in an Applet

hi,

I’m writing a simple applet (JApplet actualy) that needs to display images but I’m finding it very difficult.

My code is as follows:

import java.awt.*;  
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import java.io.*;
/**
 *
 * @author Nick
 */
public class TestJApplet extends JApplet {
    
    /** Creates a new instance of TestJApplet */
    public TestJApplet() {
    }
    
    public void init()
    {
        
    }
    
    public void paint(Graphics g)
    {
        Image image = null;
        
        try
        {
                        
            InputStream is = new BufferedInputStream(new FileInputStream("a.gif"));
            image = ImageIO.read(is);
        }
        catch(Exception e) 
        {
            System.out.println("Exception : " + e);
        }
         
        
        g.drawImage(image, 100,100,this);
        g.drawRect(30,30,30,30);
    }
       
    
}

and get the exception :

java.security.AccessControlException: access denied (java.io.FilePermission a.gif read)

I’ve also tried:

File file = new File("a.gif");
image = ImageIO.read(file);

and

image = getImage(getDocumentBase(), "a.gif");

and get the same access denied message.

Is there a simple way to get an image to load in an Applet (or JApplet)

Thanks in advance :wink:

if the file is in the applets jar (presuming you have jarred up your applet?), you should use this.getClass().getResourceasStream

I dont have too much experience working with Jar files. Am I right in thinking that the .jar file will contain .class files and not .java files? If so, how can I modify the applet after the Jar has been created? Also, do I store the images in the Jar file?

cheers

yes, a jar file is an encapsulated distribution of your application.

getResourceAsStream simply finds the first file that is present on the classpath, that matches the specified name.

If you are running your application unjarred, the current directory will be considered in the classpath too. (along with anything else you have specified)

Am I right in thinking that the .jar file will contain .class files and not .java files?

Yes.

If so, how can I modify the applet after the Jar has been created?

You like… keep the source for that case :wink:

Also, do I store the images in the Jar file?

With applets its optional, with webstart its a must. Btw it doesnt has to be the same jar. You can use one for code, one for images, one for sounds… etc. The benefit of this approach is that you can change them individually, which means that users dont need to reload that much again (only the changed jars).

If you want to create jars which arent double clickable (or change the classpath or something like that) you can just zip the files with some everyday zip application and change the extension to jar.

Some simple image loading example can be found here:
http://kaioa.com/k/jarimage.jar (doubleclickable/src included… usually you dont include the source… mind you)

Try using an ImageIcon and then calling getImage() on the icon. It works! (at least for me…)


import java.awt.*;  
import javax.swing.*;
import java.applet.*;
import javax.imageio.*;
import java.io.*;
/**
*
* @author Nick
*/
public class TestJApplet extends JApplet {
    ImageIcon icon=new ImageIcon("a.gif");
    /** Creates a new instance of TestJApplet */
    public TestJApplet() {
    }
    
    public void init()
    {
        
    }
    
    public void paint(Graphics g)
    {
        Image image = icon.getImage();
        g.drawImage(image, 100,100,this);
        g.drawRect(30,30,30,30);
    }
       
    
}

That’s pretty much as close to your original code as you will get. However, (and this is just me) wouldn’t it run a little bit faster if you declared your Image outside of any methods, then called getImage() within the init() method? This way the computer doesn’t have to copy your image over and over every frame.