Java: Applet loading images. How?

CaptainKahuna

Platinum Member
May 19, 2002
2,228
0
0
www.billda.com
I've written a Blackjack card game that works with appletviewer locally on my PC. However, I get a security error when I upload it to my webserver. All the images are in a "cards" subfolder underneath the location of the class and html file. I've looked at the Javadoc and it seems like what I have should work, but when I compile I get the following error. Help!

Card.java:76: unreported exception java.net.MalformedURLException; must be caugh
t or declared to be thrown
URL location = new URL("http://www.billda.com/java/blackjack/" + filename);
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
You have to put a try/catch block around your URL constructor, in case you happened to give it a fubar url. But follow MrChad's advice first, as I know next to nothing about Swing.
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
After looking at your code I think what you really meant was:

URL location = new URL(host + filename);

As you'll notice "URL location = new URL("http://www.billda.com/java/blackjack/" + filename);" is missing the "cards" directory that you previously mentioned.

A quick browse through the website shows its required.


But yes, simply enclosing the URL creation in a try/catch block will also be needed, as its designed to force you to handle the case that the resource you're trying to reach is unreachable.
 

CaptainKahuna

Platinum Member
May 19, 2002
2,228
0
0
www.billda.com
Originally posted by: Kilrsat
But yes, simply enclosing the URL creation in a try/catch block will also be needed, as its designed to force you to handle the case that the resource you're trying to reach is unreachable.

Alright, I put it in the try/catch block and it compiled. However now I have a access control exception when it tries to get the images that prevents my applet from loading. The error is below.

java.security.AccessControlException: access denied (java.net.SocketPermission www.billda.com resolve)
 

CaptainKahuna

Platinum Member
May 19, 2002
2,228
0
0
www.billda.com
I change to using the applet's getimage(URL) function, and it gives me the following:

java.lang.NullPointerException

at java.applet.Applet.getAppletContext(Unknown Source)

at java.applet.Applet.getImage(Unknown Source)
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Originally posted by: CaptainKahuna
Originally posted by: Kilrsat
But yes, simply enclosing the URL creation in a try/catch block will also be needed, as its designed to force you to handle the case that the resource you're trying to reach is unreachable.

Alright, I put it in the try/catch block and it compiled. However now I have a access control exception when it tries to get the images that prevents my applet from loading. The error is below.

java.security.AccessControlException: access denied (java.net.SocketPermission www.billda.com resolve)
Applets are restricted in making network connections only to the web server that they originated from. So to access a resource on www.billda.com, you would need to launch the applet from www.billda.com.

For example, I slapped together a quick image loader here:
Text

If you were to try and run this code from any website other than www.kilcli.com, you would get that access restriction. (See attached code at the bottom).

Secondly, did you verify that the URL you are trying to open exists. The fact that getImage() is failing with a null pointer says you're trying to grab something that isn't there. Like I noted before, shouldn't you have been using "host + filename" instead of ""http://www.billda.com/java/blackjack/" + filename"?