1

When I run my program as a Java Application, everything works fine. However, when I run my program as a Java Applet, the images do not load, and I get this stack trace:

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at com.asgoodasthis.squares.Tile.<init>(Tile.java:42)
at com.asgoodasthis.squares.Component.start(Component.java:80)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

I have a directory named res in my project directory, and I am loading my images like this:

public static BufferedImage tileset_terrain;

public loadImage() {
    try {
        //loading our images
        tileset_terrain = ImageIO.read(new File("res/tileset_terrain.png"));
    } catch(IOException e1) {
        e1.printStackTrace();
    }
}

So how do I get the images to load when I run my program as an applet? I am using Eclipse IDE.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Isaac Adni
  • 831
  • 4
  • 14
  • 29
  • Where does "res/tileset_terrain.png" reside? Is it on the web sever, where the applet resides or is it stored (embedded within) the Jar file itself? – MadProgrammer Jul 26 '14 at 08:57
  • @MadProgrammer I'm using eclipse, and tileset_terrain.png resides in a res folder which is inside the project folder (so I guess it would be compiled in the JAR) – Isaac Adni Jul 26 '14 at 09:06
  • Okay, you can use getClass().getResource(...) as demonstrated in my answer – MadProgrammer Jul 26 '14 at 09:08

3 Answers3

2

It's likely the image can't be accessed from its current context, remember, applets normally run in a very tight security sandbox which prevents them from accessing files on the local/client file system.

You either need to load the images from the server the applet is been loaded from (using getDocument/CodeBase or a relative URL), or based on your example, as embedded an resource, for example

tileset_terrain = ImageIO.read(getClass().getResource("/res/tileset_terrain.png"));

This assumes that the image is included within the Jar file under the /res directory.

If the image resides on the server from which the applet is been load, you could also use

try {
    URL url = new URL(getCodeBase(), "res/tileset_terrain.png");
    img = ImageIO.read(url);
} catch (IOException e) {
    e.printStackTrace();
}

Take a look at Reading/Loading images and What Applets Can and Cannot Do for more details.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Since it's an Applet and that runs in the browser hence you have to use Applet#getCodeBase() and Applet#getDocumentBase

Image image = getImage(getDocumentBase(), "tileset_terrain.png");

Find more samples Here and Here

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76
-1

The code You are using can induce many exception.

Image  = getImage(getCodeBase(), "res/tileset_terrain.png");//can beused in your code

You can try this code.

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


public class DisplayImage extends Applet {  

  Image picture;  

  public void init() {  
    picture = getImage(getDocumentBase(),"res/tileset_terrain.png");  
  }  

  public void paint(Graphics g) {  
    g.drawImage(picture, 30,30, this);  
  }  

  }  
Deepanshu J bedi
  • 1,530
  • 1
  • 11
  • 23
  • Don't load resources within paint methods, this will greatly decrease the performance of the program. There is no need for MediaTracker, as Applet implements ImageObsever and you should be calling super.paint first – MadProgrammer Jul 26 '14 at 08:53
  • Don't forget super.paint, this is still has the potential to cause some very nasty paint artefacts – MadProgrammer Jul 26 '14 at 09:03
  • 1
    I know, I'm a task master but this suppose to be a professional forum ;) – MadProgrammer Jul 26 '14 at 09:06
  • i am messed up now :(. why do i need parent's properties here?(super.paint)?@MadProgrammer – Deepanshu J bedi Jul 26 '14 at 09:16
  • You need to call super.paint(g) because it is responsible for painting other aspects of the applet, like the background and may other components...normally I wouldn't recommend overriding paint at all, especially of a top level container, but for this example, I can live with it ;) – MadProgrammer Jul 26 '14 at 09:21