Java Applets Notes:
-------------------------------------------------------------------------------------
A program that is an applet must import the applet class Applet. This class is
located in the applet package. Therefore to import this class, the syntax is
import java.applet.Applet;
When creating the applet's classname, you must use the 'extends Applet' syntax
within the declaration string. For example:
public class Name extends Applet {
...
}
An applet class has four methods that are useful overriding by your applet
sub-class. These methods are:
init
To initialize the applet each time it's loaded (or reloaded).
start
To start the applet's execution, such as when the applet's
loaded or when the user revisits a page that contains the applet.
stop
To stop the applet's execution, such as when the user leaves
the applet's page or quits the browser.
destroy
To perform a final cleanup in preparation for unloading.
These methods are not always needed by an applet subclass. Many useful applets
do not call them at all, for example an applet that simply draws itself.
The init method is where most code that would normally go into a constructor
is used. An applet is not guaranteed to have full environment until the init
method is called, applets should not usually have a constructor for this
reason.
Every applet that does something after initialization (except in direct
response to user actions) must override the start method.
Most applets that override start should also override the stop method. The
stop method should suspend the applet's execution, so that it doesn't take
up system resources when the user isn't viewing the applet's page. For
example, an applet that displays animation should stop trying to draw the
animation when the user isn't looking at it.
Many applets don't need to override the destroy method, since their stop
method (which is called before destroy) does everything necessary to shut
down the applet's execution. However, destroy is available for applets that
need to release additional resources.
-------------------------------------------------------------------------------------
The paint method is one of two display methods an applet can override:
paint
The basic display method. Many applets implement the paint
method to draw the applet's representation within a browser
page.
update
A method you can use along with paint to improve drawing
performance.
To react to an event, an applet must override either the appropriate
event-specific method or the handleEvent method. For example, adding
the following code to the Simple applet makes it respond to mouse clicks.
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
...
public class SimpleClick extends Applet implements MouseListener {
...
public void init() {
addMouseListener(this);
...
}
public void mouseEntered(MouseEvent event) {
}
public void mouseExited(MouseEvent event) {
}
public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}
public void mouseClicked(MouseEvent event) {
addItem("click!... ");
}
}
-------------------------------------------------------------------------------------
Adding User Interface (UI) components:
The java programming language offers pre-made UI components. The following
is a list of these:
Buttons (java.awt.Button)
Checkboxes (java.awt.Checkbox)
Single-line text fields (java.awt.TextField)
Larger text display and editing areas (java.awt.TextArea)
Labels (java.awt.Label)
Lists (java.awt.List)
Pop-up lists of choices (java.awt.Choice)
Sliders and scrollbars (java.awt.Scrollbar)
Drawing areas (java.awt.Canvas)
Menus (java.awt.Menu, java.awt.MenuItem, java.awt.CheckboxMenuItem)
Containers (java.awt.Panel, java.awt.Window and its subclasses)
Applet container methods (Applet inherits from the Container class):
add - Adds the specified Component.
remove - Removes the specified Component.
setLayout - Sets the layout manager.
To add a text field to the Simple applet, use the following code:
//Importing java.awt.Graphics is no longer necessary
//since this applet no longer implements the paint method.
. . .
import java.awt.TextField;
public class ScrollingSimple extends Applet {
//Instead of using a StringBuffer, use a TextField:
TextField field;
public void init() {
//Create the text field and make it uneditable.
field = new TextField();
field.setEditable(false);
//Set the layout manager so that the text field will be
//as wide as possible.
setLayout(new java.awt.GridLayout(1,0));
//Add the text field to the applet.
add(field);
validate();
addItem("initializing... ");
}
. . .
void addItem(String newWord) {
//This used to append the string to the StringBuffer;
//now it appends it to the TextField.
String t = field.getText();
System.out.println(newWord);
field.setText(t + newWord);
repaint();
}
//The paint method is no longer necessary,
//since the TextField repaints itself automatically.
After all this, the init method calls the validate method (which Applet inherits
from Component). Invoking validate once after adding one or more Components to an
applet is a bit of voodoo that ensures that the Components draw themselves onscreen.
-------------------------------------------------------------------------------------
Applet Security:
Current browsers impose the following restrictions on any applet that is loaded
over the network:
An applet cannot load libraries or define native methods.
It cannot ordinarily read or write files on the host that's executing it.
It cannot make network connections except to the host that it came from.
It cannot start any program on the host that's executing it.
It cannot read certain system properties.
Windows that an applet brings up look different than windows that an
application brings up.
Each browser has a SecurityManager object that implements its security
policies. When a SecurityManager detects a violation, it throws a
SecurityException. Your applet can catch this SecurityException and
react appropriately.
Here are some other things that current browers and other applet viewers let
applets do:
Applets can usually make network connections to the host they came from.
Applets running within a Web browser can easily cause HTML documents to
be displayed.
Applets can invoke public methods of other applets on the same page.
Applets that are loaded from the local file system (from a directory
in the user's CLASSPATH) have none of the restrictions that applets
loaded over the network do.
Although most applets stop running once you leave their page, they don't
have to.
Loading executable code over the network is a classic security risk. For
Java applets, some of this risk is reduced because the Java language is
designed to be safe -- for example, it doesn't allow pointers to random
memory. In addition, Java-compatible browsers improve security by imposing
restrictions. These restrictions include disallowing applets from loading
code written in any non-Java language, and disallowing applets from reading
or writing files on the browser's host.
-------------------------------------------------------------------------------------
Browsers and Applets:
When a Java-enabled browser encounters an