| Applets have many different activities that correspond
to various major events in the life cycle of the applet-for example, |
| 1. initialization, |
| 2. painting, |
| 3. and mouse events. |
| |
| Each activity has a corresponding method, so when an
event occurs, the browser or other Java-enabled tool calls those specific methods. |
| |
| There are five of the most important methods in an applet's execution: |
| 1. initialization, |
| 2. starting, |
| 3. stopping, |
| 4. destroying, |
| 5. and painting. |
| |
| |
| Initialization |
| Initialization occurs when the applet is first
loaded (or reloaded), similarly to the main() method
in applications. The initialization of an applet might
include reading and parsing any parameters to the applet, creating
any helper objects it needs, setting up an initial
state, or loading images or fonts. |
| |
| To provide behavior for the initialization of our
applet, override the init() method in our applet class: |
public void init()
{
...
}
Starting |
| |
| After an applet is initialized, it is started.
Starting is different from initialization because it
can happen many different times during an applet's lifetime, whereas
initialization happens only once. Starting can also occur if
the applet was previously stopped. |
| |
| For example, |
| An applet is stopped if the reader follows a link to a
different page, and it is started again when the reader returns to this page. |
| To provide startup behavior for
your applet, override the start() method: |
public void start()
{
...
}
Stopping |
| |
| |
| Stopping |
| Stopping occurs when the reader leaves the page
that contains a currently running applet, or you can
stop the applet yourself by calling stop(). By default, when
the reader leaves a page, any threads the applet had s
tarted will continue running. |
| |
| By overriding stop(), you can suspend execution
of these threads and then restart them if the applet is viewed again: |
public void stop()
{
...
} |
| |
| |
| Destroying |
| Destroying enables the applet to clean up after itself
just before it is freed or the browser exits-for example, to
stop and remove any running threads, close any open network
connections, or release any other running objects.
Generally, we won't want to override destroy() unless we
have specific resources that need to be released-for
example, threads that the applet has created. |
| |
| To provide clean-up behavior for your applet, override the destroy() method: |
public void destroy()
{
...
} |
| |
| |
| Painting |
| Painting is how an applet actually draws something on
the screen, be it text, a line, a colored background, or an image.
Painting can occur many thousands of times during an applet's life .
We override the paint() method if your applet needs to have an actual
appearance on the screen (that is, most of the time). |
| |
| The paint() method looks like this: |
public void paint(Graphics g)
{
...
} |
| |
| paint() takes an argument, an instance of the class Graphics.
This object is created and passed to paint by the browser, |
| |
| |
| import java.awt.Graphics; |
A Simple Applet
The Hello Again applet
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
public class Aman extends java.applet.Applet
{
Font f = new Font("TimesRoman", Font.BOLD, 36);
public void paint(Graphics g)
{
g.setFont(f);
g.setColor(Color.red);
g.drawString("Hello again!", 5, 40);
}
} |
|
|