small question about Images in Applet

hello!

I want to use some images in an Applet and i use this code:

Image IMGlightON = Toolkit.getDefaultToolkit().getImage(new java.net.URL("http://10.0.0.40/lightON.jpg" ) );

Image IMGlightOFF = Toolkit.getDefaultToolkit().getImage(new java.net.URL("http://10.0.0.40/lightFF.jpg" ) );

and i get these errors in compiler:

unreported exception java.net.MalformedURLException; must be caught or declared to be thrown

unreported exception java.net.MalformedURLException; must be caught or declared to be thrown

can someone please explain me what i do wrong because i can't

understand...

thanks alot!

[778 byte] By [StorM_GmAa] at [2007-11-27 6:09:15]
# 1
It means that one of the methods on that line throws a MalformedURLException, so you have to either try/catch it, or throw it from the method it's in. http://java.sun.com/docs/books/tutorial/essential/exceptions/
hunter9000a at 2007-7-12 17:12:47 > top of Java-index,Java Essentials,Java Programming...
# 2
1.) Wrap it in a try block2.) Applet.getImage() should be your first choice3.) You shouldn't use absolute URLs as Applets aren't allowed to access servers that differ from their home
quittea at 2007-7-12 17:12:47 > top of Java-index,Java Essentials,Java Programming...
# 3

can you tell me please how the try/catch should look like? i don't know

what exception should i put in catch...

furthermore, i didn't understand the

"3.) You shouldn't use absolute URLs as Applets aren't allowed to access servers that differ from their home"

what kind of URL should i use? that URL is from my local PC but

because i can't use images right from my machine to a Java Applet,

i tried to use them through my Apache server...

sorry if i'm annoying you guys but I'm new in Java so I can't easily get

what you're talking about...

StorM_GmAa at 2007-7-12 17:12:47 > top of Java-index,Java Essentials,Java Programming...
# 4

> can you tell me please how the try/catch should look

> like? i don't know

> what exception should i put in catch...

Follow that link I gave you, specifically the second section:

"This section covers how to catch and handle exceptions. The discussion includes the try, catch, and finally blocks, as well as chained exceptions and logging."

hunter9000a at 2007-7-12 17:12:47 > top of Java-index,Java Essentials,Java Programming...
# 5

> what kind of URL should i use? that URL is from my local PC but

> because i can't use images right from my machine to a Java Applet,

> i tried to use them through my Apache server...

You could [url=http://java.sun.com/docs/books/tutorial/deployment/jar/]pack them in a jar file[/url], and then [url=http://java.sun.com/docs/books/tutorial/uiswing/components/icon.html]use them via the class.getResource() method[/url].

An applet is not allowed, unless you sign it, to access files on the local computer, and it is also not allowed to connect to any server but the server that the applet was downloaded from. Thus, you pack the whole thing in a jar file, code and images and all, and the applet is happy again.

If the images are already up on the same webserver that you'll be putting the applet on, you can just use the Applet's getImage (as stated a few posts up) method to get the image off the server, and you don't have to worry about packaging and getting resources.

> sorry if i'm annoying you guys but I'm new in Java so I can't easily get

> what you're talking about...

No problem... if you were that annoying, I guess I'd just close the window ;)... we were all new to this once, too.

kevjavaa at 2007-7-12 17:12:47 > top of Java-index,Java Essentials,Java Programming...
# 6

> what kind of URL should i use? that URL is from my local PC but

> because i can't use images right from my machine to a Java Applet,

> i tried to use them through my Apache server...

This method is what I mean:

http://java.sun.com/j2se/1.5.0/docs/api/java/applet/Applet.html#getImage(java.net.URL,%20java.lang.String)

You get a ready-made URL object by calling this one:

http://java.sun.com/j2se/1.5.0/docs/api/java/applet/Applet.html#getDocumentBase()

or this one (whatever suits your needs):

http://java.sun.com/j2se/1.5.0/docs/api/java/applet/Applet.html#getCodeBase()

quittea at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 7
Just my two bits, but I avoid the Applet methods so that I can reuse the code in a non-applet.Not that there's anything wrong with the Applet methods per se.
Hippolytea at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 8

erhm...i think things are getting more complicated than i expected!

and all these for an university assignment...blah!

anyway, it took me so long to reply because that Jar thing was looking

nice but i counldn't understand where to type the command to create

the Jar!

however, I think it's better to find out a way to communicate my applet

with my PC because along with the images, I want to make some

Rankings as well (the whole thing is a Game Applet so i wanted to

create rankings with scores) thus i need Applet to write files on my

PC and HTML can read them to put the rankings into the local website.

but our point is not the Rankings right now but the images!

Jar would be a good way to work with the images but i will not be able

(i guess) to do the Rankings thing. So i think that "sign" you guys told

me could be better...I think it's something like i tell to the program that

it can trust my PC and get the images and stuff...

and now comes the question...how i can do that?

i appreciate your help! I really do!

StorM_GmAa at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 9
> i counldn't understand where to type the command to createthe Jar!Gosh. What operating system are you using? Windows? Have you never used the command window?
Hippolytea at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 10
http://java.sun.com/developer/Books/javaprogramming/JAR/sign/signing.html
quittea at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 11
i am using Windows XP and for Java i am using JCreator LE 4.00what command window? you mean DOS? o.O
StorM_GmAa at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 12
> what command window? you mean DOS? o.OYes, the windows command shell aka DOS box/prompt.
quittea at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 13

ok that's wierd! it might sound noobish but how DOS could understand

the command "jar"?! i used this format of command

jar cf jar-file input-file(s)

of course i replaced everything needed replacement but my DOS says

that "jar" cannot be recognized as a command etc etc...in a link you

guys gave me i saw that i need the JDK for the Jar and i have it...

StorM_GmAa at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 14
The folder containing the program jar.exe is not on your PATH.One option is to type in the whole path:"/Program Files/Java/jdk1.6.0/bin/jar" cf ...Your path may be different. The quotes are because of that darn space in "Program Files"
Hippolytea at 2007-7-12 17:12:48 > top of Java-index,Java Essentials,Java Programming...
# 15

The command shell searches a few well-known system paths for an executable named jar[.suffix] and doesn't succeed, because the place where it is (directory "bin" of your JDK location) is not among these paths. You need to either state the full path or add it to the "PATH" environment variable.

http://vlaurie.com/computers2/Articles/environment.htm

quittea at 2007-7-21 21:43:36 > top of Java-index,Java Essentials,Java Programming...
# 16

> however, I think it's better to find out a way to communicate my applet

> with my PC because along with the images, I want to make some

> Rankings as well (the whole thing is a Game Applet so i wanted to

> create rankings with scores) thus i need Applet to write files on my

> PC and HTML can read them to put the rankings into the local website.

When you want to send information to a server it ceasing being just an applet

question and become more of a server-side question. First, choose the

server technology you are happiest with, say web services + web app.

Then work on communicating with the server. Don't assume the answer

is "signed applet".

Hippolytea at 2007-7-21 21:43:36 > top of Java-index,Java Essentials,Java Programming...