Image loading troubles

Hi, everyone. Nice little forum you have here! :)

I'm quite the n00b to making games, but am trying my best. I can get everything to work fine using AppletViewer, but whenever I try to use a web browser, an exception is thrown at the following location in my code:

static String bgFile = "map.jpg";

Image bgImage = Toolkit.getDefaultToolkit().getImage(bgFile);

The exception that is thrown is as follows:

java.security.AccessControlException: access denied (java.io.FilePermission map.jpg read)

I've done quite a bit of research, but I can't really find a way around it. Again, everything works fine when run using Appletviewer, so I suppose browser applets want me to grant it access to read files in a given directory. If it makes any difference, this code is in a class MapWindow that extends JFrame.

Help is much appreciated! When I have something to show, I'll gladly post my game here for you to enjoy. :)

[971 byte] By [SpikesCafe] at [2007-9-27 13:59:57]
# 1

Applet has a getImage(URL) method. Try using that instead.

*******************************************************************************

Answer provided by Friends of the Water Cooler. Please inform forum admin via the

'Discuss the JDC Web Site' forum that off-topic threads should be supported.

YATArchivist at 2007-7-5 21:49:32 > top of Java-index,Other Topics,Java Game Development...
# 2

I tried getImage(URL) a bunch of different ways, but now it throws lots of errors, even with AppletViewer. Besides, I'd rather keep clear of creating all my images in the Applet-extending class and keep them localized to their respective classes; if I just can't get it to work any other way, I'll spend more time with your method. I *really* appreciate your help!! I'm going to try using this MediaTracker thing to see if it can make it work right. :)

SpikesCafe at 2007-7-5 21:49:32 > top of Java-index,Other Topics,Java Game Development...
# 3
Create a jar file with your classes and images and reference that. Then use (untested):Image im = applet.getImage( applet.getCodebase(), "MyImage.jpg" );
beggarstune at 2007-7-5 21:49:32 > top of Java-index,Other Topics,Java Game Development...
# 4

> Image bgImage =

> Toolkit.getDefaultToolkit().getImage(bgFile);

> ...

> I'm going to try using this MediaTracker thing to see

> if it can make it work right. :)

Here is a method I use in my applets and applications to load images:

public Image loadImage(String path, Component parent) throws Exception

{

MediaTracker imageTracker = new MediaTracker(parent);

URL url = parent.getClass().getResource(path);

Image img = Toolkit.getDefaultToolkit().createImage(url);

imageTracker.addImage (img, 0);

imageTracker.waitForID(0);

return img;

}

jlbpa at 2007-7-5 21:49:32 > top of Java-index,Other Topics,Java Game Development...
# 5

Oh, wow, thanks so much! Sorry I didn't reply sooner, I figured this thread was done for. I did figure out how to get the images to load on my applet, and it is actually along similar lines that you get yours.

public ImageSet(String spriteLocation)

//CONSTRUCTOR

{

Image portrait = Toolkit.getDefaultToolkit().getImage(

getClass().getResource(spriteLocation+"portrait0.jpg"));

}

I very much appreciate your help, jlbpa. I am saving your method in case I want to change things around later. :)

SpikesCafe at 2007-7-5 21:49:32 > top of Java-index,Other Topics,Java Game Development...