Loading Image in Applet

im trying to load an image into an applet with the following code:

File file =new File(path);

try{Picture = ImageIO.read(file);}

catch (IOException e){e.printStackTrace();}

it doesnt give an error there so the path and the rest works.

it gives an error when it is being drawn(loaded). eclipse console:

Exception in thread"AWT-EventQueue-1" java.lang.NullPointerException

at Main.paint(Main.java:57)

at sun.awt.RepaintArea.paintComponent(Unknown Source)

at sun.awt.RepaintArea.paint(Unknown Source)

at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)

at java.awt.Component.dispatchEventImpl(Unknown Source)

at java.awt.Container.dispatchEventImpl(Unknown Source)

at java.awt.Component.dispatchEvent(Unknown Source)

at java.awt.EventQueue.dispatchEvent(Unknown Source)

at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.pumpEvents(Unknown Source)

at java.awt.EventDispatchThread.run(Unknown Source)

and that repeating everytime it calls the paint method.

i think it is because of the sandbox. i don't want to sign my applet yet, i want to work with the .policy documents. I found this Eclipse-Auto-Created document next to my classes (java.policy.applet):

/* AUTOMATICALLY GENERATED ON Tue Apr 16 17:20:59 EDT 2002*/

/* DO NOT EDIT */

grant{

permission java.security.AllPermission;

};

When i run the policytool.exe i find this in the warning log:

java.io.FileNotFoundException: C:\Documents and Settings\MyName\.java.policy (The system cannot find the file specified)

Now the Question:

Whats wrong with eclipses policy file and how do i fix it (if thats the problem)?

Thanks for reading all this :)

[2405 byte] By [Comp-Freaka] at [2007-11-27 9:07:01]
# 1

Hello!

I think the problem is not the policy, I think the problem is on the path to the image. Are you using windows or linux/unix?

My suggestion is that you create a folder inside you eclipse project called: img

and under it you put the image. In the path variable you should provide the relative path to the file

Here is a link about relative and absolute paths

Best regards

skalstera at 2007-7-12 21:43:12 > top of Java-index,Java Essentials,Java Programming...
# 2

Im (very)sorry to inform you that the problem didnt lie there :(.

i tried it with various paths realativ&absolute. i even tried faking the path just to see what would happen. the eclipse response to a fake path:

javax.imageio.IIOException: Can't read input file!

at javax.imageio.ImageIO.read(Unknown Source)

at Tile.Load(Tile.java:39)

at Main.init(Main.java:23)

at sun.applet.AppletPanel.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

im using windows xp. thanks for your concern

Comp-Freaka at 2007-7-12 21:43:12 > top of Java-index,Java Essentials,Java Programming...
# 3

Applets are not permitted to read files (except in very special circumstances). So, even if you got the file path right it might work under AppletViewer but you'll get a security exception under a browser.

You should load any images from the server from which the applet's code was read. For fixed images use Class.getResource() (or ResourceAsStream()).

malcolmmca at 2007-7-12 21:43:12 > top of Java-index,Java Essentials,Java Programming...
# 4

Im using eclipse appletviewer (does that count?). I knew about that and i read that if you want to test your applet you can create some kind of policy files which allo all applets from a path/url to do what they want. i didn't find anything else on that though...

// Edit

i heard that with

getimage(base,imagename) you can get the image.

The problem is that i want to load an image from inside my class which doesnt extend applet. Any ideas?

Message was edited by:

Comp-Freak

Comp-Freaka at 2007-7-12 21:43:12 > top of Java-index,Java Essentials,Java Programming...
# 5

> Im using eclipse appletviewer (does that count?). I

> knew about that and i read that if you want to test

> your applet you can create some kind of policy files

> which allo all applets from a path/url to do what

> they want. i didn't find anything else on that

> though...

>

Yes, you can change your applet policy file, but what's the point? You have to do it on the client system that's running the browser. If you have to modify the client system that defeats the whole point of using an applet (i.e. not having to install stuff), and you might as well get the extra flexibility of running a straight GUI program.

> // Edit

> i heard that with

> getimage(base,imagename) you can get the image.

> The problem is that i want to load an image from

> inside my class which doesnt extend applet. Any

> ideas?

>

Sure, but it depends if the image is a "resource". A resource is essentially a file which is a fixed part of the program. If it's a resource you do something like:

Icon image1 = new ImageIcon(getClass().getResource("myimage.gif"), "Icon");

This requires the file myimage.gif to sit next to the .class file of the class in question. It can be online, in a jar, or both. GetResource uses the same mechanism to get to the file that the JVM does to access the .class files.

malcolmmca at 2007-7-12 21:43:12 > top of Java-index,Java Essentials,Java Programming...
# 6
Sorry that im so totally dumb but i still have these questions: - My class 'cant reslove icon and imageicon to a type...' - Can resources be inside folders (but still in the classes directory)? - what does the "Icon" signify? What if it's a picture?
Comp-Freaka at 2007-7-12 21:43:12 > top of Java-index,Java Essentials,Java Programming...