I have a small game of memory cards which I got working properly, but I want to put on the web. I followed the basic guidelines, changed the constructor to init(), removed SetSize and SetTitles, removed references to closeWindow or system.exit(0), and thought that should be enough, but it does not work. After uploading to the server, it will not load and appears to give an access denied/file permission IIO error. (http://homepage.mac.com/aparentdesign/design/code.html)
All class files are in thee same directory as the HTML. I am worried it is not accessing the images in their folders, even though they also are in the same directory as the HTML and the relative link path should be the same no matter where it is. I am able to view the graphics as a URL, so I don't think it's .mac preventing access to the images. (http://homepage.mac.com/aparentdesign/design/random/0.jpg )
My other thought is that it does not like the timer method I used to pause the game?
Is there anything else that JApplets don't like? I've been searching Sun's tutorials and haven't found anything that seems to address this.
Since I don't know where exactly the problem may lie, I'm gonna have to paste the whole thing... sorry!
One thing I see right off the bat is the ImageIcon constructor calls: you're using the constructor that tries to use a local file path. This isn't going to work because when the applet runs on a desktop, the images aren't on the local machine; they're on the server. You can instead use the ImageIcon constructor that takes a URL for an argument. You can get the base URL by calling getDocumentURL() in the applet, and then build up the full URLs relative to that.
I thougth I didn't need to use URLs, since the relative links are the same, they are in the same directory on server. (If this were html, it would have no difficulty accessing the images). Also, if I attempt to run this locally (off a browser, just dragging the html file to the browser) it doesn't render either, and that is local, and relative.
researching further tho, thanks for the lead.
ok, I searched on URLs, searched on getDocumentURL, and hulkSmashed some code together, which still doesn't work. I am testing this locally from my hard drive, but as an applet in aHTML. It compiles, but displays this error onscreen:
Java Security AccesControlException: AccessDenied (java io FilePermission incredi/backgroundWin.jpg read) which is the first image that is attempted to access. [ May 20, 2005: Message edited by: A Parent ]
Suggestions — 1 — allow java to do the heavy lifting for you Ernest's suggestion
this one will fetch images from a jar file and works for both an applet context and as an app, ie when you run the applet from a main method. For more about this see the How to Use Icons page in the tutorial.
2 — you only need to load the images into the applet one time.
thanks craig... the first hint helped a lot, and now the applet will run on the web. However, now I'm seeing other errors. The first time the game is played, all is well,... but the second time, when a match is found, it's resetting to the values the cards had in the first game.
So I'm guessing my reset method needs some kind of clear cache? I searched for a clear cache method but didn't find anything.
We didn't go over jar files in class at all, so I'm unfamiliar with them... and not sure how you mean that I only need to load the images once? There are more images than the game needs, so each time the game is played it needs to load the ones it wants that time?
You can load the images into the applet at startup and store them in an array. Then in your reset method, instead of loading in the images all over again for the new icons (easy on your hard drive but takes more bandwidth/time over the network) you can get them from the array