Chapter Seven Applet
Chapter Seven Applet
Applets
Applet Basics
Applets are small programs that are designed for
transmission over the Internet and run within a browser
or Applet Viewer tool(JDK)
Applet does not have a main( ) method.
applets use the interface provided by the AWT.(not I/O
console)
Application vs. Applet
Application
Trusted (i.e., has full access to system resources)
Invoked by Java Virtual Machine (JVM, java), e.g.,
Should contain a main method, i.e.,
public static void main(String[])
Applet
Not trusted (i.e., has limited access to system resource to prevent
security breaches)
Invoked automatically by the web browser
Should be a subclass of class java.applet.Applet
Examining a simple applet
// A minimal applet.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Java makes applets easy.", 20, 20);
}
}
Cont…
This applet begins with two import statements:
first imports the Abstract Window Toolkit (AWT) classes.
Import statement imports the applet package.(This package contains
the class Applet. Every applet that you create must be a subclass of
Applet.)
Inside SimpleApplet, paint( )is declared.
defined by the AWT Component class (which is a superclass of Applet)
must be overridden by the applet.
It is called or invoked when :
Each time the applet must redisplay its output.
The applet begins execution
Redraw its output
The paint( )method has one parameter of type Graphics.
Cont….
Inside paint( ), there is a call to drawString( ), which is a member
of the Graphics class.
It has the following general form:
void drawString(String message, int x, int y)
Message is the string to be output beginning at x,y.
Graphics Coordinate
(0,0) x
height
y
width
Embedding Applet into HTML
To execute an applet (in either a Web browser or the
appletviewer), you need to write a short HTML text file that
contains the appropriate APPLET
For example :for the above applet code, HTML file that will
execute SimpleApplet is
<applet code="SimpleApplet" width=200 height=60>
</applet>
And you have to save it with SimpleApplet.html
We can use stand-alone HTML file to execute an applet(both
the code and the html code).
Compiling and Running
To compile
javac SimpleApplet.java
Produces SimpleApplet.class
To run
Open page SimpleApplet.htmlfrom web browser or
Use appletviewer of JDK
appletviewer SimpleApplet.html
The window produced by SimpleApplet, as displayed by
applet viewer, is shown in the following illustration:
A Complete Applet Skeleton
applets override a set of methods form Applet class // start or resume execution
These five methods can be assembled into the skeleton shown }
here: // Called when the applet is stopped.
// An Applet skeleton. public void stop() {
import java.awt.*; // suspends execution
import java.applet.*; }
/* /* Called when applet is terminated. This is the last
<applet code="AppletSkel" width=300 height=100> method executed. */
</applet> public void destroy() {
*/ // perform shutdown activities
public class AppletSkel extends Applet { }
// Called first. // Called when an applet's window must be restored.
public void init() { public void paint(Graphics g) {
// initialization // redisplay contents of window
} }
/* Called second, after init(). Also called whenever }
the applet is restarted. */
public void start() {
Applet Initialization and Termination
When an applet begins, the following methods are called in
this sequence:
1. init( )
2. Start( )
3. Paint ( )
When an applet is terminated the following methods are
called in this sequence :
1. Stop ( )
2. destroy( )
The Life-Cycle of Applet
init()
Called exactly once in an applet’s life.
Called when applet is first loaded,e.g., when the
browser visits the web page for the first time.
Used to initialize variables, read applet parameters,
start downloading any other images or media files, etc.
start( )
The start( )method is called after init( ).
It is also called to restart an applet after it has been
stopped.
start( ) is called each time an applet’s HTML document
is displayed onscreen.
The Life-Cycle cont….
paint( )
is called each time your applet’s output must be redrawn.
has one parameter of type Graphics.
This context is used whenever output to the applet is
required.
stop( )
is called when a web browser leaves the HTML document
containing the applet
• destroy()
Called exactly once.
Called when the browser unloads the applet.
Used to perform any final clean-up.
The stop( )method is always called before destroy( ).
The Life-Cycle cont….
init
start destroy
stop
start
Code Example …
/* A simple applet that sets the foreground and msg = "Inside init( ) --";
background colors and outputs a string. */ }
import java.awt.*; // Initialize the string to be displayed.
import java.applet.*; public void start() {
/* msg += " Inside start( ) --";
<applet code="Sample" width=300 }
height=50> // Display msg in applet window.
</applet> public void paint(Graphics g) {
*/ msg += " Inside paint( ).";
public class Sample extends Applet{ g.drawString(msg, 10, 30);
String msg; }
// set the foreground and background colors. }
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
The output
Color Control
Exaple : setColor(Color.RED); or
setColor(new Color(255,0,0));
Font Class
Fonts are specified with three attributes:
font name:
Serif Sans-serif Monospaced Dialog DialogInput
TimesRoman Helvetica Courier
font style:
PLAIN BOLD ITALIC
Styles can be combined: Font.BOLD|Font.ITALIC
font size: a positive integer