in my applet I can't get the stop() and the start() to fire

F

Flip

I cannot seem to see when the stop() and start() methnods fire when using
this applet. I thought when the focus moved away from IE6, the stop would
fire and then when it got the focus again the start() would fire. Is that
not the case?

Any ideas on what I could be doing wrong? Thank you.

--------------code
snippet----------------------------------------------------------

package com.pch.webapp;

import java.applet.Applet;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Toolkit;

public class ScrollingMessage extends Applet implements Runnable{
String message;
private int dx;
private int dy;
private boolean isStandalone = false;
private boolean running;
private Thread worker;
private int x = 25;
private int y = 25;

public ScrollingMessage(){
}

public static void main( String[] args ){

ScrollingMessage applet = new ScrollingMessage();
applet.isStandalone = true;

Frame frame;
frame = new Frame();
frame.setTitle( "Applet Frame" );
frame.add( applet, BorderLayout.CENTER );
applet.init();
applet.start();
frame.setSize( 400, 320 );

Dimension d = Toolkit.getDefaultToolkit()
.getScreenSize();
frame.setLocation(
( d.width - frame.getSize().width ) / 2,
( d.height - frame.getSize().height ) / 2
);
frame.setVisible( true );
}

// Destroy the applet
public void destroy(){
System.err.println( "destroy" );
worker = null;
}

// Get Applet information
public String getAppletInfo(){
System.err.println( "getAppletInfo" );

return "Applet Information";
}

public int getDx(){

return dx;
}

public int getDy(){

return dy;
}

public String getMessage(){

return message;
}

// Get a parameter value
public String getParameter( String key, String def ){

return isStandalone ? System.getProperty( key, def )
: ( ( getParameter( key ) != null ) ? getParameter( key ) :
def );
}

// Get parameter info
public String[][] getParameterInfo(){
System.err.println( "getParameterInfo" );

String[][] pinfo = {{"aMessage", "String", ""}
, };

return pinfo;
}

public int getX(){

return x;
}

public int getY(){

return y;
}

// Initialize the applet
public void init(){
System.err.println( "init" );
try{
message = this.getParameter( "aMessage", "There's no place like
127.0.0.1." );
} catch( Exception e ){
e.printStackTrace();
}

try{
jbInit();
} catch( Exception e ){
e.printStackTrace();
}
}

public void paint( Graphics g ){
System.err.println( "painting" );
g.drawString( this.getMessage(), getX(), getY() );
}

public void run(){
while( isRunning() ){
setX( getX() + 1 );
setDx( getDx() );
this.repaint();
try{
System.err.println( "\tabout to sleep" );
worker.sleep( 250 );
} catch( InterruptedException ex ){
ex.printStackTrace();
}
}

System.err.println( "done" );
}

public void setDx( int dx ){
this.dx = dx;
}

public void setDy( int dy ){
this.dy = dy;
}

public void setX( int x ){
this.x = x;
}

public void setY( int y ){
this.y = y;
}

// Start the applet
public void start(){
System.err.println( "start" );
if( worker == null ){
worker = new Thread( this );
}

setRunning( true );
worker.start();
}

// Stop the applet
public void stop(){
System.err.println( "stop" );
if( worker != null ){
setRunning( false );
}
}

private boolean isRunning(){

return running;
}

// Component initialization
private void jbInit() throws Exception{
}

private synchronized void setRunning( boolean running ){
this.running = running;
}

}
 
G

Guest

Flip said:
I cannot seem to see when the stop() and start() methnods fire when using
this applet. I thought when the focus moved away from IE6, the stop would
fire and then when it got the focus again the start() would fire. Is that
not the case?

Any ideas on what I could be doing wrong? Thank you.

--------------code
snippet----------------------------------------------------------

package com.pch.webapp;

import java.applet.Applet;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Toolkit;

public class ScrollingMessage extends Applet implements Runnable{
String message;
private int dx;
private int dy;
private boolean isStandalone = false;
private boolean running;
private Thread worker;
private int x = 25;
private int y = 25;

public ScrollingMessage(){
}

public static void main( String[] args ){

ScrollingMessage applet = new ScrollingMessage();
applet.isStandalone = true;

Frame frame;
frame = new Frame();
frame.setTitle( "Applet Frame" );
frame.add( applet, BorderLayout.CENTER );
applet.init();
applet.start();
frame.setSize( 400, 320 );

Dimension d = Toolkit.getDefaultToolkit()
.getScreenSize();
frame.setLocation(
( d.width - frame.getSize().width ) / 2,
( d.height - frame.getSize().height ) / 2
);
frame.setVisible( true );
}

// Destroy the applet
public void destroy(){
System.err.println( "destroy" );
worker = null;
}

// Get Applet information
public String getAppletInfo(){
System.err.println( "getAppletInfo" );

return "Applet Information";
}

public int getDx(){

return dx;
}

public int getDy(){

return dy;
}

public String getMessage(){

return message;
}

// Get a parameter value
public String getParameter( String key, String def ){

return isStandalone ? System.getProperty( key, def )
: ( ( getParameter( key ) != null ) ? getParameter( key ) :
def );
}

// Get parameter info
public String[][] getParameterInfo(){
System.err.println( "getParameterInfo" );

String[][] pinfo = {{"aMessage", "String", ""}
, };

return pinfo;
}

public int getX(){

return x;
}

public int getY(){

return y;
}

// Initialize the applet
public void init(){
System.err.println( "init" );
try{
message = this.getParameter( "aMessage", "There's no place like
127.0.0.1." );
} catch( Exception e ){
e.printStackTrace();
}

try{
jbInit();
} catch( Exception e ){
e.printStackTrace();
}
}

public void paint( Graphics g ){
System.err.println( "painting" );
g.drawString( this.getMessage(), getX(), getY() );
}

public void run(){
while( isRunning() ){
setX( getX() + 1 );
setDx( getDx() );
this.repaint();
try{
System.err.println( "\tabout to sleep" );
worker.sleep( 250 );
} catch( InterruptedException ex ){
ex.printStackTrace();
}
}

System.err.println( "done" );
}

public void setDx( int dx ){
this.dx = dx;
}

public void setDy( int dy ){
this.dy = dy;
}

public void setX( int x ){
this.x = x;
}

public void setY( int y ){
this.y = y;
}

// Start the applet
public void start(){
System.err.println( "start" );
if( worker == null ){
worker = new Thread( this );
}

setRunning( true );
worker.start();
}

// Stop the applet
public void stop(){
System.err.println( "stop" );
if( worker != null ){
setRunning( false );
}
}

private boolean isRunning(){

return running;
}

// Component initialization
private void jbInit() throws Exception{
}

private synchronized void setRunning( boolean running ){
this.running = running;
}

}
Use
new Thread(this).start();
in place of
applet.start();
 
F

Flip

Thank you for the response.
Use
new Thread(this).start();
in place of
applet.start();

I'm seeing a compile error.

"ScrollingMessage.java": non-static variable this cannot be referenced from
a static context at line 99, column 19

And I thought the main method wasn't used in IE when the applets are run,
no?
 
G

Guest

Flip said:
Thank you for the response.




I'm seeing a compile error.

"ScrollingMessage.java": non-static variable this cannot be referenced from
a static context at line 99, column 19

And I thought the main method wasn't used in IE when the applets are run,
no?

I'm talking rubbish. I was feeling a bit post trigger happy this morning :)

Having fully read your original posting, the start() method is called
when the applet is originally loaded just after init() is called.

When a user moves to another page the stop() method is called.

When they return to the page (possibly by using the back button) the
start() method is called again.

The start and stop methods are not called when the browser focus is lost
or gained. You may be able to implement some kind of Focus listener,
I've not tried it.
 
J

John C. Bollinger

nik[no-spam]cross said:
Having fully read your original posting, the start() method is called
when the applet is originally loaded just after init() is called.
Yes.

When a user moves to another page the stop() method is called.

Maybe. Historically, not all browsers have implemented this behavior.
However, it is certainly the case that that is where stop() is
_supposed_ to be invoked.
When they return to the page (possibly by using the back button) the
start() method is called again.

Maybe. If the applet was stop()ped but held, then it's start() method
must be invoked in this case. If the applet was not stop()ped and is
used again for the page view then it's start() method will not be
invoked. If a new applet instance is used then it will be init()ed and
then start()ed.
The start and stop methods are not called when the browser focus is lost
or gained. You may be able to implement some kind of Focus listener,
I've not tried it.

Yes, that would be the appropriate way of approaching it. Focus
behavior is a bit tricky, but you may be able to get it to work that way.


The lifecycle of an Applet plays out within this framework:

Immediately after instantiation, an Applet is in the "uninitialized" state.

init(): prepares an Applet instance for use [one-time initialization].
Will only be invoked on an applet in the "uninitialized" state, and
moves it to the "ready" state.

start(): sets an Applet running after init() or stop() [may be invoked
many times]. Will only be invoked on an applet in the "ready" state,
and moves it to the "running" state.

An applet must be in the "running" state to present its UI.

stop(): causes an Applet to stop running (whatever that means for the
Applet in question). Will only be invoked on an applet in the "running"
state, and moves it to the "ready" state.

destroy(): causes the applet to perform any necessary cleanup prior to
being discarded. Will only be invoked on an applet in the "ready"
state, and moves it to the "destroyed" state.

Sun may use different names for the lifecycle states -- I didn't look
them up because the names don't really matter. Note that this model
presents a severe difficulty for a host web browser, in that there is no
good, general way to determine, on the second and subsequent views of a
page containing a reference to particular Applet, whether the expected
behavior is reuse of the same applet instance or use of a new instance.
That being the case, applet developers _must_ be prepared for the
possibility that a new instance will be created and used (after
init()ing and start()ing it) for each page view, as well as for the
possibility that a previously used instance will be re-start()ed and
used (but not re-init()ed!). As far as I have ever been able to
determine, either behavior is perfectly valid.


John Bollinger
(e-mail address removed)
 
A

Andrew Thompson

On Mon, 06 Sep 2004 10:34:30 -0500, John C. Bollinger wrote:

(snip applet intricacies)
That being the case, applet developers _must_ be prepared for the
possibility that a new instance will be created and used (after
init()ing and start()ing it) for each page view, as well as for the
possibility that a previously used instance will be re-start()ed and
used (but not re-init()ed!). As far as I have ever been able to
determine, either behavior is perfectly valid.

That and about a dozen or two other
applet/browser interaction quirks that
come down to not just the JRE and browser
type, but browser version (and OS) and the
browser plug-ins installed, such as the
Google toolbar.

Aaah.. Applets.
Gotta love 'em, ..or 'go postal'. ;-)
 
F

Flip

Thanks everyone for your comments!:>

Andrew Thompson said:
On Mon, 06 Sep 2004 10:34:30 -0500, John C. Bollinger wrote:

(snip applet intricacies)

That and about a dozen or two other
applet/browser interaction quirks that
come down to not just the JRE and browser
type, but browser version (and OS) and the
browser plug-ins installed, such as the
Google toolbar.

Aaah.. Applets.
Gotta love 'em, ..or 'go postal'. ;-)

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top