0% found this document useful (0 votes)
16 views27 pages

Applet

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

Applet Programming

• Applets are small Java programs that are primarily


used in Internet computing.
• They can be transported over the Internet from one
computer to another computer and run using the
Applet Viewer or any Web browser that supports Java.
• An applet, like any application program, can do many
things for us.
• It can perform arithmetic operations, display graphics,
play sound, accept user input, create animation, and
play interactive games.
• Java has enabled web browsers to create and use fully
interactive multimedia web documents.
Local and remote applets
• We can embed applets into Web pages in two ways.
– One, we can write our own applets and embed them into Web
pages.
– Second, we can download an applet from a remote computer
system and then embed it into a web page.
• An applet developed locally and stored in a local system is
known as a local applet.
• When a web page is trying to find a local applet, it does
not require the Internet connection. It simply searches the
directories in the local system and locates and loads the
specified applet.
How applets differ from applications?
Java Applets and Java applications have many similarities but there are some
differences between applets and applications.

1. Applets do not use main() method but applications have main() method.

2. Unlike stand-alone applications, applets cannot be run independently. They


are run from inside a Web page using a special feature known as HTML tag.

3. Applets cannot read from or write to the files in the local computer.

4. Applets cannot communicate with other servers on the network.

5. Applets cannot run any program from the local computer.

6.Applets are restricted from using libraries from other languages such as C or
C++.(Java language supports this feature through native methods)
When to use applets?
• When we need something dynamic to be
included in the display of a Web page.
• When we require some “flash” outputs. For
eg:, applets that produce sounds, animations
or some special effects would be useful when
displaying certain pages.
• When we want to create a program and make
it available on the Internet .
Steps involved
• Before we try to write applets, we must make sure that
Java is installed properly and also ensure that either Java
appletviewer or a Java-enabled browser is available.
Steps
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 the Web page
6. Creating HTML file
7. Testing the applet code
Building applet code
• Applet code uses the services of two classes – Applet and Graphics from the Java
class library.
• The Applet class which is contained in the java.applet package provides life and
behavior to the applet through its method such as init(), start(), and paint().
• Unlike the applications, where Java calls the main() method directly to initiate the
execution of the program, when an applet is loaded, Java automatically calls a
series of Applet class methods for starting, running, and stopping the applet code.
• The Applet class therefore maintains the life cycle of an applet.
• The paint() method of Applet class, when it is called, actually displays the result of
the applet code on the screen.
• The output may be text, graphics, or sound.
• The paint() method, which requires a Graphics object as an argument, is defined
as follows:
public void paint( Graphics g)
• This requires that the applet code imports the java.awt package that contains the
Graphics class.
• All output operations of an applet are performed using the methods defined in
the Graphics class.
General form:
import java.awt.*;
import java.applet.*;
---------------------
---------------------
public class appletclassname extends Applet
{
-----------------
-----------------
public void paint (Graphics g)
{
--------------
-------------- //Applet operations code
}
------------------
------------------
}
• The appletclassname is the main class for the applet.
• When an applet is loaded, Java creates an instance of this class, and then a
series of Applet class methods are called on that instance to execute the
code.
The HelloJava applet
import java.awt.*;
import java.applet.*;
public class HelloJava extends Applet
{
public void paint(Graphics g)
{
g.drawString("HelloJava", 10, 100);
}

}
• When executed , draws the string “Hello Java” at the position 10, 100 (pixels)
• Java requires that the main applet class be declared public.
Applet Life Cycle
• When an applet is loaded, it undergoes a
series of changes in its state.
• The applet states include:
1. Born or initialization state
2. Running state
3. Idle state
4. Dead or destroyed state
Applet Life Cycle

Begin
Load Applet

Destroyed

Stopped Exit from


Display
browser
Initialization state
• Applet enters initialization state when it is first loaded.
• This is achieved by calling the init( ) method of Applet class. The applet is
born.
• At this stage, we may do the following, if required:
1. Create objects needed by the applet
2. Set up initial values
3. Load images or fonts
4. Set up colors
• The initialization occurs only once in the applet’s life cycle.
• To provide any of the behaviours mentioned above, we must override the
init() method:
public void init()
{
--------------(Action)
}
Running state
• Applet enters the running state when the system calls the start() method of
Applet class.
• This occurs automatically after the applet is initialized.
• Starting can also occur if the applet is already in “stopped” (idle) state.
• For example, we may leave the Web page containing the applet temporarily
to another page and return back to the page. This again starts the applet
running.
• Unlike init() method, the start() method may be called more than once.
• We may override the start()method to create a thread to control the applet.
public void start()
{
----------(Action)
}
Idle or Stopped state
• An applet becomes idle when it is stopped from running.
• Stopping occurs automatically when we leave the page
containing the currently running applet.
• We can do so by calling the stop() method explicitly.
• If we use a thread to run the applet, then we must use stop()
method to terminate the thread.
• We can achieve this by overriding the stop() method.
public void stop()
{
---------------(Action)
}
Dead state
• An applet is said to be dead when it is removed from memory.
• This occurs automatically by invoking the destroy() method
when we quit the browser.
• Like initialization, destroying stage occurs only once in the
applet’s life cycle.
• If the applet has created any resources, like threads, we may
override the destroy() method to clean up these resources.
public void destroy()
{
-----------(Action)
}
Display state
• Applet moves to the display state whenever it has to perform some output
operations on the screen.
• This happens immediately after the applet enters into the running state.
• The paint() method is called to accomplish this task.
• Almost every applet will have a paint() method.
• Like other methods in the life cycle, the default version of paint() method does
absolutely nothing.
• We must override this method if we want anything to be displayed on the
screen.
public void paint(Graphics g)
{
--------------(Display statements)
}
• Display state is not considered as a part of applet’s life cycle.
Creating an executable applet
• Executable applet is the .class file of the applet, which is obtained by
compiling the source code of the applet.
• Compiling an applet is exactly the same as compiling an application.
• We can use the Java compiler to compile the applet.
• Steps required for compiling the HelloJava applet.
1. Move to the directory containing the source code and type the following
command:
javac HelloJava.java
2. The compiled output file called HelloJava.class is placed in the same
directory as the source.
3. If any error message is received, then we must check for errors, correct
them and compile the applet again.
Designing a web page
• In order to run a Java applet, it is first
necessary to have a Web page that references
that applet.
• A web page is basically made up of text and
HTML tags that can be interpreted by a Web
browser or an applet viewer.
• HTML files should be stored in the same
directory as the compiled code of the applets.
Applet tag
• Include a pair of <APPLET ….> and <APPLET> tags in the body section.
• The <APPLET…> tag supplies the name of the applet to be loaded and tells the browser
how much space the applet requires.
• The ellipsis in the tag <APPLET…> indicates that it contains certain attributes that must be
specified.
• The <APPLET> tag given below specifies the minimum requirements to place the HElloJava
applet on a Web page:
<APPLET
CODE=HelloJava.class
WIDTH=400
HEIGHT=200>
</APPLET>
• This HTML code tells the browser to load the compiled Java applet HelloJava.class, which
is in the same directory as the HTML file.
• Also specifies the display area for the applet output as 400 pixels width and 200 pixels
height.
Adding applet to HTML file
<html>
<head>
<title>Welcome to Java Applets</title>
</head>
<body>
<center>
<h1>Welcome to the world of Java Applets</h1>
</center>
<br>
<center>
<applet
code=HelloJava.class
width=400
height=200>
</applet>
</center>
</body>
</html>
• We must name this file as “HelloJava.html” and save it in the same file as the compiled applet.
Running the applet
• To run an applet, we require one of the following tools:
1. Java enabled Web browser(such as HotJava or Netscape)
2. Java appletviewer
• The appletviewer is not a full-fledged Web browser
and therefore it ignores all of the HTML tags except
the part pertaining to the running of the applet.
• We can use run an applet using appletviewer as
follows:
appletviewer HelloJava.html
More about applet tag
• Syntax
<APPLET
[CODEBASE = codebaseURL]
CODE = appletFileName.class
[ALT = alternateText]
[NAME = appletInstanceName ]
WIDTH = pixels
HEIGHT = pixels
[ALIGN = alignment]
[ VSPACE = pixels]
[ HSPACE = pixels] >
[<PARAM NAME = appletAttribute1 VALUE = value>]
[<PARAM NAME = appletAttribute2 VALUE = value>] . . .
[Text to be displayed in the absence of Java]
</APPLET>
Attribute Value Description

left
right
top Specifies the alignment of an applet according to
align
bottom surrounding elements
middle
baseline

alt text Specifies an alternate text for an applet


archive URL Specifies the location of an archive file
Specifies a relative base URL for applets specified
codebase URL
in the code attribute
height pixels Specifies the height of an applet
hspace pixels Defines the horizontal spacing around an applet
name name Defines the name for an applet (to use in scripts)
vspace pixels Defines the vertical spacing around an applet
width pixels Specifies the width of an applet
Steps
1. Insert an <applet> tag at an appropriate place in the Web page.
2. Specify the name of the applet’s .class file.
3. If the .class file is not in the current directory, use the codebase
parameter to specify
• The relative path if file is on the local system, or
• The URL of the directory containing the file if it is on a remote computer.
4. Specify the space required for display of the applet in terms of
width and height in pixels.
5. Add any user-defined parameters using <param> tags.
6. Add alternate HTML text to be displayed when a non-Java browser
is used.
7. Close the applet declaration with the</applet> tag.
Passing parameters to Applets
• We can supply user-defined parameters to an applet using<PARAM…> tags.
• Each <PARAM…> tag has a name attribute such as color, and a value
attribute such as red.
• Inside the applet code, the applet can refer to that parameter by name to
find its value.
• For eg:, we can change the color of the text displayed to red by an applet
by using a <PARAM…> tag as follows:
<APPLET>
<PARAM name=“color” value =“red”>
</APPLET>
• We can change the text to be displayed by an applet by supplying new text
to the applet through a <PARAM…> tag as:
<PARAM name=“text” value=“I love Java”>
HelloJavaParam.java
import java.awt.*;
import java. applet.*;
public class HelloJavaParam extends Applet
{
String str;
public void init()
{
str=getParameter("string"); //Receiving parameter value
if(str == null)
str="Java";
str="Hello"+str; //using the value
}
public void paint(Graphics g)
{
g.drawString(str,10,100);
}
}
<html>
HelloJavaParam.html
<head>
<title>
Welcome to Java Applets</title>
</head>
<body>
<applet code="HelloJavaParam.class"
width=400
height=200>
<param name="string"
value="Applet!">
</applet>
</body>
</html>
• Parameters are passed on an applet when it is loaded.
• We can define the init() method in the applet to get hold of the parameters defined in the
<PARAM> tags.
• This is done using the getParameter() method, which takes one string argument representing
the name of the parameter and returns a string containing the value of that parameter.

You might also like