This section focuses on the construction of Java applets. The previous section discussed in some detail the process required to load an applet over the Internet to the local machine, and this section will explain what is necessary for the applet to actually run on that local machine.
Java applets are subclassed from the Appletclass in the java.appletpackage.
Each applet has four major events in its lifetime: - Initialization
- Starting
- Stopping
- Destroying
These four events correspond directly to four methods within the Applet class: init(), start(), stop(), and destroy(). The following discussion provides some information on each of these methods.
public void init()
The init() method is called when the applet is initially loaded. This method is used to do one-time setup features such as add components to the layout manager, set screen colors, and connect to a host database.
public void start()
The start() method is called after the applet has been initialized, and also each time the applet is restarted after being stopped. Applets can be stopped when the user changes to another Web page. If the user returns at a later time to the page with the applet on it, the applet will be restarted by the browser. Therefore, the start()method can be called many times during an applet's life cycle. Common operations that occur during an applet's start()method are the initialization of threads within the applet and the updating of the screen display.
public void stop()
The stop() method is called whenever the user leaves the current page. Note that by default when the user leaves a page, the applet will continue to run. This can result in an enormous consumption of system resources if many applets have been loaded and these applets are performing some resource-intensive task such as animation. (In fact, it is quite common to see poorly written applets loaded from the Internet that obviously did not implement this method. They never stop running!) The stop() method is used to temporarily suspend the execution of the applet until the start() method is called again.
public void destroy()
The destroy() method is called whenever it is time to completely finish the applet's execution. This method is generally called when the browser is exiting or the applet is being reloaded from the host server. The destroy()method is used to free up allocated resources such as threads or database connections.
Listing 7.1 shows a simple applet that implements all four life cycle methods: init(), start(), stop(), and destroy(). This applet updates the screen as well as the browser status bar with some information indicating which method is being called.
Listing 7.1. An applet illustrating the life cycle methods.
import java.awt.Graphics;
import java.awt.Font;import java.awt.Color;
public class LifeCycleApplet extends java.applet.Applet{ Font theFont = new Font("Helvetica", Font.BOLD, 20); int i; String String1, String2;
public void paint(Graphics g) { g.setFont(theFont); g.setColor(Color.blue); g.drawString(String1 + String2, 5, 30); }
public void init() { i = 0; String1 = ""; String2 = "The applet is initializing!"; repaint(); showStatus("The applet is initializing!"); }
public void start() { i = 1; String1 = String2; String2 = "The applet is starting!"; repaint(); showStatus("The applet is starting!"); }
public void stop() { i = 2; String1 = String2; String2 = "The applet is stopping!"; repaint(); showStatus("The applet is stopping!"); }
public void destroy() { i = 3; String1 = String2; String2 = "The applet is being destroyed!"; repaint(); showStatus("The applet is being destroyed!"); }}
The previous example will always show the last two life cycle events on the screen. Because of the speed at which Java executes, you were probably unable to see the init()method's results all by themselves. We could have added a counter loop just to stall the applet, so feel free to do so if you're interested. Using threads, we also could have called a wait()method to stop program execution for a specified amount of time.
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
public class LifeCycleApplet extends java.applet.Applet
{
Font theFont = new Font("Helvetica", Font.BOLD, 20);
int i;
String String1, String2;
public void paint(Graphics g)
{
g.setFont(theFont);
g.setColor(Color.blue);
g.drawString(String1 + String2, 5, 30);
}
public void init()
{
i = 0;
String1 = "";
String2 = "The applet is initializing!";
repaint();
showStatus("The applet is initializing!");
}
public void start()
{
i = 1;
String1 = String2;
String2 = "The applet is starting!";
repaint();
showStatus("The applet is starting!");
}
public void stop()
{
i = 2;
String1 = String2;
String2 = "The applet is stopping!";
repaint();
showStatus("The applet is stopping!");
}
public void destroy()
{
i = 3;
String1 = String2;
String2 = "The applet is being destroyed!";
repaint();
showStatus("The applet is being destroyed!");
}
}
No comments:
Post a Comment