How would I make something like a high scores function on a java applet? Like how would I record the score the person got and then access later?

Recommended Answers

All 13 Replies

Where do you want to save the scores? Do you have a server and a database?

This is a pretty difficult problem, actually. The applet security model is based on the applet running in total isolation, so it can't really communicate with the world. There is a way to do it, but it's very clunky and really a last-ditch solution. I understand that servlets offer some better optione in this regard, but I don't know much about that side of things.

I'd suggest you look into servlets, though.

Due to Java security reason if a class extends Applet it cann't:
save any records on a file
open and read a file, and
alter a file.
However it may record scores in an array. As long as the program runs, one may access the data. If the program terminates all the scores recorded are lost.

What do you mean, "If the program terminates?" If I had it embedded in a web page and someone stopped using it would that be terminated? Would this be easier to do this in javascript? And thanks for the suggestion of servletts, I'll look into it.

"Terminate" means cann't restart. One cann't use Applet method void start() to restart. One has to use init() to start a new run.

In other words, if your player closes the browser window and returns, his high scores are gone, if you just rely on methods within the applet.

(It's also possible to run as a "trusted" applet - I've never looked into this, because it never seemed very promising to me, but I'm sure you can find out more with some well-worded searches)

Would it be possible to store values in a text file on my computer and access them through the applet?

I think it's possible for something like this to happen under a trusted applet, but again, I have never really looked into the details (applet programming is something I've only really done once, and I'm not likely to do any more in the near future, except for the most trivial sort),

You've got two computers involved, the client side and the server side. The applet runs on the client-side machine. It would be convenient to have access to memory there, but that's exactly what's forbidden under the applet model (with the possible exception of the trusted scenario). It's tempting then to try to use the server-side computer - you just make a database available, and have the applet call up that database to read or store, right?
No.
The security model prevents this. Try it - we tried everything we could think of to talk to the home base from a running applet, and none of it worked. We tried making an HTTP connection - no go. We tried sending an email. No go. We tried everything we could think of, and the language designers had thought of and blocked all of them. We were unable to talk to the server while the applet was running.

No. It is the security rule: Applet cann't do any operation associated with files, such as create, open, read, delete, and alter in your computer.

Well, there's the rub: while the applet is running, it can't communicate.
However, the applet can communicate with the outside world on two occasions. Any guesses on when it can be done?

It is impossible by Java applet class itself to store data at the client computer due to the restriction of Java security.
However, applet class may communicate with its own server. Any demon program runs in the server may receive any information sent by the applet class at the client's computer. In this way the demon program at the server may store the data at server since the demon program does not extend Applet. Also the demon program may pass any information from one client's computer to another client's computer at different location.

When I googled "record java applet," this is one of the results. There are many more.This discussion belongs in the MozillaZine Tech forum

Would it be possible to store values in a text file on my computer and access them through the applet?

Yes. You can set it up so that you can write on your computer with your applet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.