Sunteți pe pagina 1din 42

Applets

Attitude is a
small

thing, but it can make

BIG
difference.
Introduction
 Applets are small Java programs
 Used in Internet computing
 Can be transported over Internet
 Run using AppletViewer or Web Browser
Difference between Applications and Applets
 1. Applets can be embedded in HTML pages
and downloaded over the Internet whereas
Applications have no special support in HTML
for embedding or downloading.
 2. Applets can only be executed inside a java
compatible container, such as a browser or
appletviewer whereas Applications are
executed at command line by java.exe.
 3. Applets execute under strict security
limitations that disallow certain operations
whereas Applications have no inherent
security restrictions.
 4. Applets don’t have the main() method as in
applications. Instead they operate on an
entirely different mechanism where they are
initialized by init(),started by start(),stopped
by stop() or destroyed by destroy().
 5. Applets can not read from or write to the
files in the local computer.
 6. Applets can not communicate with the
other servers on the network.
 7. Applets can not run any program from the
local computer.
Steps for writing and Testing Applets
1. Building an Applet code(.java file)
2. Creating an executable applet(.class file)
3. Designing a web page using HTML tags
4. Preparing <APPLET…> Tag
5. Incorporating <APPLET…> Tag into web
page
6. Creating a HTML file
7. Testing the applet code
Building an Applet code
 Applet code uses methods from two classes:
Applet and Graphics
 The Applet class contained in java.applet
package includes init(), start() and paint()
methods. Java automatically calls a series
of Applet class methods for starting, running
and stopping the applet code.
 The Applet class maintains the lifecycle of
an applet.
The paint() method, when called, displays
the result of the applet on the screen. The
output may be text, graphics or sound. The
paint() method requires a graphics object as
an argument-
public void paint( Graphics g)
This requires the import of java.awt
package that contains the Graphics class.
All output operations of an applet are
performed using methods defined in the
Graphics class.
General form of Applet code
import java.applet.*;
import java.awt.*;
…………..;
…………..;
public class Appletclassname extends Applet
{
…………..;
…………..;
public void paint (Graphics g)
{
……………….;
}
}
 The following is a simple applet named
WelcomeJavaApplet.java:
import java.applet.*;
import java.awt.*;
public class WelcomeJavaApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Welcome to Java", 25, 50);
}
}
 Graphics.drawString() :
The drawSring() method draws the given string.
Here is the syntax of the drawString() method :

drawString(String string, int X, int Y);

 Graphics.drawLine() :
The drawLine() method is used to draw the line
in the applet. Here is the syntax for the
drawLine() method :

drawLine(int X1 , int Y1 , int X2 , int Y2 );


 Graphics.drawOval() :
The drawOval() method draws the circle. Here is the
syntax of the drawOval() method:

g.drawOval(int X, int Y, int Wdth, int height);

 Graphics.drawRect() :
The drawRect() method draws the rectangle. Here
is the syntax of the drawRect() method:

g.drawRect(int X, int Y, int Wdth, int height)


 setColor() method:
This method sets the color for the object by
specified color. Here is the syntax of the
setColor() method :

g.setColor(Color.color_name);

 Graphics.fillOval() :
This is the fillOval() method used to fill the color
inside the oval by specified color. Here is the
syntax of the fillColor() method :

g.fillOval(Color.color_name);
 fillRect() :
This is the fillRect() method used to fill the color
inside the rectangle by specified color. Here is
the syntax of the fillRect() method :

g.fillRect(int X, int Y, int Wdth, int height)

 drawArc():

void drawArc( int x, int y,


int width, int height,
int startAngle, int arcAngle)
Font:
Font(String name, int style, int size)
name- Serif, SansSerif, etc
style- Font.BOLD, Font.ITALIC, Font.PLAIN
eg
g.setFont(new font(“Serif”, Font.BOLD, 30));
g.drawString(“Pune”, 50, 50);
g.setFont(new font(“SansSerif”, Font.ITALIC, 30));
g.drawString(“Mumbai”, 50, 100);
 background colour:
void setBackground(Color.blue);
void setBackground(new Color(127,255,212));

 Foreground color:
void setForeground(Color.white);
import java.awt.*;
import java.applet.*;
/* <applet code="WelcomeApplet" width=200
height=200> </applet> */
public class WelcomeApplet extends Applet {
public void paint(Graphics g) {
g.setColor(Color.yellow);
setBackground(Color.blue);
g.setFont(new Font("Arial", Font.BOLD, 40));
g.drawString("Welcome to Java Applets", 70, 70);
}
}
Applet Life Cycle
 Every applet inherits set of default behavior
from the Applet class.
 When an applet is loaded it undergoes
series of changes in its state.
 The applet states include-
 Born or Initialization state
Running state
Idle state
Dead or Destroyed state
Applet’s state transition diagram

Begin Born Initialization
(Load Applet)

start()
stop()

Display Running Idle Stopped

start()
paint() destroy()

Destroyed Dead End

Exit of Browser
Initialization state
 Applets enter initialization state when it is
first loaded, by calling init() method of
Applet class. The applet is born. At this
stage, we can
Create objects needed by applet
Set initial values
Load images or fonts
Set colours
The initialization occurs only once.
To provide any of the behaviours listed
above, init() method must be overridden.
public void init()
{
----------------;
----------------;//action statements
----------------;
}
Running state
 Applet enters running state when system
calls start() method of Applet class.
This happens automatically after the
applet is initialized.
The start() method can be called more
than once. It may be overridden.
public void start()
{
----------------;
----------------;//action statements
----------------;
}
Idle or Stopped state
An applet becomes idle when it is stopped
from running.
The stop() method can be overridden.

public void stop()


{
----------------;
----------------;//action statements
----------------;
}
Dead state
An applet is said to be dead if it is removed
from memory.
This occurs automatically by invoking
destroy() method when the browser is exited.
This occurs only once. It frees the recourses.
The destroy() method can be overridden.
public void destroy()
{
----------------;
----------------;//action statements
----------------;
}
Display state
 Applet moves to display state when it has to
perform output operations on screen. This
happens immediately after the applet enters
into the running state. The paint() method is
called for this.
The paint() method must be overridden.
public void paint()
{
----------------;
----------------;//action statements
----------------;
}
 Display state is not considered as a part
of applet’s life cycle.
The paint() method is defined in Applet
class.
It is inherited from Component class, a
super class of class Applet.
Creating an executable applet
Java compiler can be used to compile the
applet.
Let the source file is HelloJava.java
Steps to compile HelloJava.java-
 Move to the directory containing source code
and type the command
javac HelloJava.java
 The compiled file HelloJava.class is placed in
the same directory as source.
If any error message is received, correct it and
recompile.
The HTML <APPLET…> Tag
 The APPLET tag is used to start an applet
from both an HTML document and from an
applet viewer.
An applet viewer will execute each APPLET
tag that it finds in a separate window, while
web browsers like Netscape Navigator,
Internet Explorer, and HotJava will allow
many applets on a single page.
 The syntax for the standard APPLET tag-
< APPLET
[CODEBASE = codebaseURL]
CODE = appletFile.class
[ALT = alternateText]
[NAME = appletInstanceName]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[VSPACE = pixels] [HSPACE = pixels]
>
[< PARAM NAME = AttributeName VALUE = AttributeValue>]
[< PARAM NAME = AttributeName2 VALUE = AttributeValue>]
...
[HTML Displayed in the absence of Java]
</APPLET>
CODEBASE: an optional attribute that
specifies the base URL of the directory in
which applet resides. If the applet resides
in the same directory as the html file,
CODEBASE can be omitted.
The CODEBASE need not be on the host
from which the HTML document was read.
CODE: CODE is a required attribute that
gives the name of the file containing your
applet’s compiled .class file.
ALT: The ALT tag is an optional attribute
used to specify a short text message that
should be displayed if the browser
understands the APPLET tag but can’t
currently run Java applets.
Non Java browsers will display this text.
NAME: NAME is an optional attribute used
to specify a name for the applet instance.
Applets must be named in order for other
applets on the same page to find them by
name and communicate with them.
WIDTH AND HEIGHT: WIDTH and HEIGHT
are required attributes that give the size (in
pixels) of the applet display area.
ALIGN: ALIGN is an optional attribute that
specifies the alignment of the applet.
 The possible values: LEFT, RIGHT, TOP,
BOTTOM, MIDDLE, BASELINE, TEXTTOP,
ABSMIDDLE, and ABSBOTTOM.
VSPACE AND HSPACE: These attributes
are optional. VSPACE specifies the space, in
pixels, above and below the applet. HSPACE
specifies the space, in pixels, on each side of
the applet.

PARAM NAME AND VALUE : The PARAM


tag allows to specify applet specific
arguments in an HTML page. Applets access
their attributes with the getParameter( )
method.
getDocumentBase() and getCodeBase()
The applets may need to explicitly load
media and text.
Java allows the applet to load data from
the directory holding the HTML file that
started the applet (the document base)
and the directory from which the applet’s
class file was loaded (the code base).
These directories are returned as URL
objects by getDocumentBase() and
getCodeBase().
URL getCodeBase()- gets the Base URL
URL getDocumentBase()- gets the URL of
the document in which this applet is
embedded
They can be concatenated with a string that
names the file you want to load. To actually
load another file, use the showDocument()
method defined by the AppletContext I/F.
AppletContext and showDocument()
 One application of Java is to use active
images and animation to provide a
graphical means of navigating the Web.
 To allow your applet to transfer control to
another URL, you must use the
showDocument( ) method defined by the
AppletContext interface.
AppletContext is an interface that lets
you get information from the applet’s
execution environment.
The context of the currently executing
applet is obtained by a call to the
getAppletContext( ) method defined by
Applet.
Within an applet, once you have obtained
the applet’s context, you can bring another
document into view by calling
showDocument( ).
There are two showDocument( ) methods.
The method showDocument(URL) displays the
document at the specified URL.
The method showDocument(URL, where)
displays the specified document at the specified
location within the browser window.
 Valid arguments for where are “_self”
(show in current frame), “_parent” (show in
parent frame), “_top” (show in topmost
frame), and “_blank” (show in new browser
window).
You can also specify a name, which
causes the document to be shown in a
new browser window by that name.
Passing Parameters to Applet
The user defined parameters can be supplied
to the applets using <PARAM…> tags.
Each <PARAM…> tag has a name attribute,
such as color and a value attribute such as
blue.
Inside the applet code, the applet can refer to
that parameter by name to find its value.
<applet … >
<param color value=“green"/>
</applet>
To set up and handle parameters user need
to do-
Include appropriate <PARAM…> tags in the HTML
document
Provide code in the applet to parse these
parameters, if required.
 Parameters are passed to the applet when it
is loaded. We can define init() method to get
hold of parameters defined in <PARAM> tags.
This is done using getParameter() method
which takes one string argument representing
the name of the parameter and returns a
string containing the value of that parameter.

S-ar putea să vă placă și