This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Applets and the fly likes Standalone app worked... as Applet does not. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Standalone app worked... as Applet does not. " Watch "Standalone app worked... as Applet does not. " New topic
Author

Standalone app worked... as Applet does not.

A Parent
Greenhorn

Joined: May 18, 2005
Posts: 9
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!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
A Parent
Greenhorn

Joined: May 18, 2005
Posts: 9
Thanks for responding!

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.

[edit]

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 ]
A Parent
Greenhorn

Joined: May 18, 2005
Posts: 9
here is the relevant code I added, and I changed all references to folder1/folder2 to be the URLS.

Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Suggestions —
1 — allow java to do the heavy lifting for you
Ernest's suggestion

another possibility

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.
A Parent
Greenhorn

Joined: May 18, 2005
Posts: 9
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?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
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

[ June 02, 2005: Message edited by: Craig Wood ]
A Parent
Greenhorn

Joined: May 18, 2005
Posts: 9
wow, that does seem much simpler. ::scampers off to edit code::

do you think that would solve my cache issue?
A Parent
Greenhorn

Joined: May 18, 2005
Posts: 9
huh. I had a reply here, and now it's gone.

anyway, I'm still working on making the code change you suggested--but am not sure if that would effect the cache issue?

btw, this is no longer a "school project" but is now on my own time and curiosity.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Standalone app worked... as Applet does not.