Java Applet Question

Status
Not open for further replies.

strategist333

In Runtime
Messages
103
Hi all

I have a major project due in three days, and I need to figure out how to terminate an applet once the browser is closed. The applet runs just fine, but because I use a while(true) loop to generate random iterations, the applet keeps on running even though I tell Internet Explorer to exit. I don't think that I can simply use a for loop, because the point of my program is to keep going until told to stop (it is a simple screensaver-like applet). My teacher will not accept projects that require a "Ctrl+alt+del">>terminate IEXPLORE.exe to exit out of the program. Is there any way (code) to remedy this problem? Thanks!!

Here's a rough outline of my code::

public class Project extends Applet
{
public void init()
{....}
public void paint(Graphics p)
{
while(true)
{
//draws objects onto a screen
}
}
//other methods, etc.
}
 
hmm, tricky one - i wouldn't have thought it was possible to capture a web browser closing event so getting this right might be a bit of a hack. Generally speaking infinite loops are poor coding so as csamuels hints at you need some condition that may exits the loop.

My initial thought is if you have a loop like you do why not just chuck a timer in there too: Presumably you have some action listenter somewher or some way for the user to interact with the applet - if you reset the timer each time an action happens then you can time out the applet at say 10minutes of inactivity, or whatever you see suitable. i.e.

Code:
while(time<X){
    /* do some stuff */
}

Although clearly this is a bit of a hack and there is the potential for a user to open loads of instances of this applet in a very short space of time and potentially starving the server of resources for 10 minutes....

Ideally you need a way of determining if the browser window is open or not, otherwise if there is something in your code which is only ever going to happen if the user has navigated away - perhaps even just opening it in another popup window - this way you can probably tell if the window is open or closed... possibly! - having not written many applets i cant say any more. good luck though! let me know how you get on
 
csamuels said:

How would a do while help my program exit? (If I set the limit incredibly high, the applet still will not exit)

Also, my teacher wants us to use only "stuff that we have already learned," and we haven't learned ActionListener yet. Is there any easy way to detect browser closing? or should I just run the program using a for loop for a short time?
 
Use whatever is at your disposal. My AP Comp sci teacher was annoying like that too, she'd get mad if you used something not in the textbook. I fought her - I got something low (if not a 0) on the assignment, but I felt I deserved it. Plus it was my senior year in HS, whatever.
 
Status
Not open for further replies.
Back
Top Bottom