<<

WRITING A

A is an object of superclass java.applet.Applet.

The life cycle of an applet moves through these stages:

It initializes It starts executing It stops executing It is destroyed

These life cycle stages are triggered by the in response to its user:

When the user opens the referencing the applet, the applet initializes and starts executing. When the user moves to another web page, or exits the browser, the applet stops and is destroyed. When the user returns to the web page, a new applet object is initialized and started. When the user refreshes the web page, the current applet object is stopped and destroyed and a new object is initialized and started.

For each stage, class Applet declares a corresponding method that the JVM calls when the applet reaches that stage. Here they are:

java.applet.Applet Life Cycle Methods Method Called by the JVM when… void init( ) The applet initializes. void start( ) The applet starts executing. void stop( ) The applet stops executing. void destroy( ) The applet is destroyed.

Writing a Java Applet Page 1 To write an applet, declare a subclass of Applet and morph whichever method gives the behavior you want.

Example To see the applet stages in action, copy this applet to jGRASP and compile and execute it there. You do not need an associated HTML file to run in jGRASP. Observe when the methods are called during the applet's life cycle. 1 import java.applet.Applet; 2 import static javax..JOptionPane.*; 3 4 public class AppletLifeCycle extends Applet 5 { 6 public String name; // shared by methods 7 8 public void init( ) 9 { 10 name = showInputDialog( "What's your name?" ); 11 } 12 13 public void start( ) 14 { 15 showMessageDialog( null, "Hello, " + name ); 16 } 17 18 public void stop( ) 19 { 20 showMessageDialog( null, "Goodbye, " + name ); 21 } 22 23 public void destroy( ) 24 { 25 showMessageDialog( null, "ARRRRGGGGG!" ); 26 } 27 }

Writing a Java Applet Page 2 An applet doesn't have System.in and System.out available to it like an application does but you can do input and output from and to JOptionPane dialog boxes.

With this in mind, you can generally rewrite any Java application as an applet. The exception is an application that stores data onto the host computer; this can only be done by an applet under very strict conditions.

Example Write a Java applet that converts U. S. dollars to Japanese yen. For example, $10.00 = ¥931.10. import java.applet.Applet; import static javax.swing.JOptionPane.*; import java.text.DecimalFormat; public class DollarToYenApplet extends Applet { // applet data final double YEN_PER_DOLLAR = 76.56; // $1 = ¥76.56 DecimalFormat dfus = new DecimalFormat( "$#,###.00" ); DecimalFormat dfjp = new DecimalFormat( "\u00A5#,###.00" );

public void init( ) { String input, output; double usd, jpy; input = showInputDialog( "Enter U.S. $ " ); usd = Double.parseDouble( input ); jpy = usd * YEN_PER_DOLLAR; output = dfus.format( usd ) + " = " + dfjp.format( jpy ); showMessageDialog( null, output ); } }

Writing a Java Applet Page 3 Exercises

Use jGRASP to enter, save and compile the AppletLifeCycle applet. Answer the questions below.

1. Click the Run . What happens? Why?

2. Close the message dialog. Minimize the applet window. What happens? Why?

3. Close the message dialog. Restore the applet window. What happens? Why?

4. Close the message dialog. Close the applet window. What happens? Why?

5. Use jGRASP to enter, save and compile the DollarToYenApplet applet. Run the applet. What is the output for an input of 100 dollars?

6. Write an HTML file that is appropriate for running the DollarToYenApplet applet. Load the HTML file into . What is the output for an input of 150 dollars? What happens when you click the browser's reload button? Why?

Writing a Java Applet Page 4