Applet Slides
Applet Slides
Applet Slides
Applet Basics
Applets are not stand-alone programs. Instead, they run within
either a web browser or an applet viewer. The illustrations
shown are created with the standard applet viewer, called
appletviewer, provided by the JDK. But we can use any
applet viewer or browser we like.
Execution of an applet does not begin at main( ).Instead,
execution of an applet is started and controlled with an entirely
different mechanism.
Applet Basics
To use an applet, it is specified in an HTML file. One way to do this is by
using the APPLET tag.
The applet will be executed by a Java-enabled web browser when it
encounters the APPLET tag within the HTML file.
To run an applet from a browser, you need to create an HTML file with the
<applet>tag. Here is an example of such a comment:
/*
<applet code="MyApplet" width=200 height=60>
</applet>
*/
<applet>is the tag name, and code, width, and height are
attributes. The width and height attributes specify the rectangular viewing
area of the applet.
An Applet Skeleton
init( )
The init method is invoked after the applet is created.
This method is called only once during the run time of your applet.
start( )
The start( ) method is called after init( ). It is also called to restart an applet
after it has been stopped. Whereas init( ) is called oncethe first time an
applet is loadedstart( ) is called each time an applets HTML document
is displayed onscreen. So, if a user leaves a web page and comes back, the
applet resumes execution at start( ).
An Applet Skeleton
stop( )
The stop( ) method is called when a web browser leaves the HTML
document containing the appletwhen it goes to another page, for
example. When stop( ) is called, the applet is probably running.
You should use stop( ) to suspend threads that dont need to run when the
applet is not visible. You can restart them when start( ) is called if the user
returns to the page.
destroy( )
The destroy( ) method is called when the environment determines that your
applet needs to be removed completely from memory. At this point, you
should free up any resources the applet may be using.
The stop( ) method is always called before destroy( ).
An Applet Skeleton
// An Applet skeleton.
import javax.swing.JApplet;
/* <applet code="AppletSkel" width=300 height=100> </applet> */
public class AppletSkel extends JApplet
{
// Called first.
public void init() {
// initialization
}
/* Called second, after init(). Also called whenever the applet is restarted. */
public void start() {
// start or resume execution
}
// Called when the applet is stopped.
public void stop() {
// suspends execution
}
/* Called when applet is terminated. This is the last method executed. */
public void destroy() {
// perform shutdown activities
}
Key Event
Key events enable the use of the keys to control and perform actions or get
input from the keyboard.
A key event is fired whenever a key is pressed, released, or typed on a
component.
Java provides the KeyListener interface to handle key events.
Every key event has an associated key character or key code that is
returned by the getKeyChar() or getKeyCode() method in KeyEvent.
import javax.swing.JApplet;
public class MyApplet extends JApplet { }
MyApplet.html
<applet
code=MyApplet.class
width=200 height=100>
</applet>
To execute an applet
appletviewer
15