Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 05-11-2006, 06:41 PM   #1 (permalink)
 
True Techie

Join Date: Jun 2005

Posts: 103

strategist333

Exclamation Java Applet Question

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.
}
strategist333 is offline  
Old 05-11-2006, 11:01 PM   #2 (permalink)
office politics's Avatar
 
It's all just 1s and 0s

Join Date: Jan 2004

Location: in the lab

Posts: 4,425

office politics will become famous soon enough

Default

i would use a do while loop.

http://java.sun.com/docs/books/tutor...lts/while.html

Quote:
You use a while statement to continually execute a block of statements while a condition remains true. The following is the general syntax of the while statement.

while (expression) {
statement
}

office politics is offline  
Old 05-12-2006, 06:20 AM   #3 (permalink)
 
Ultra Techie

Join Date: Oct 2003

Posts: 544

fitzjj

Default

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
fitzjj is offline  
Old 05-12-2006, 07:33 AM   #4 (permalink)
 
True Techie

Join Date: Jun 2005

Posts: 103

strategist333

Default

Quote:
Originally posted by csamuels
i would use a do while loop.

http://java.sun.com/docs/books/tutor...lts/while.html
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?
strategist333 is offline  
Old 05-16-2006, 01:33 AM   #5 (permalink)
 
Ultra Techie

Join Date: Apr 2005

Posts: 950

M4A1 is on a distinguished road

Send a message via AIM to M4A1
Default

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.
M4A1 is offline  
 
Closed Thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On