Java Unit4 AppletProgramming
Java Unit4 AppletProgramming
Applet: Applets are the small java programs that are primarily used in Internet Computing
.they can be transferred from one system to another. To run an applet we use Applet viewer or
java enabled web browser. By using applets we can develop programs which can display
graphics, play sound, create animations and play games.
Local and Remote Applets: We can embed applets into web pages in two ways
1. Local Applet
2. Remote Applet
Local Applet: We can write our own applet and embed them into webpages. An applet
developed and stored in local computer is local applet. Local Applet doesn’t need an internet
connection. It simply searches local directories to load and run applet.
Applet tag for local applets
<applet codebase= “path” code= “HelloJava.class” width = “100” height= “200” > </applet>
path – path of an applet on the local system
Remote Applet: We can download an applet from a remote computer system and then embed
it into a web page. A remote applet is developed by someone and stored on a remote computer
over internet. URL stands for Uniform Resource Locator which can specify the address of
applet on the web
Applet tag for accessing remote applet
<applet codebase= “URL” code = “Abc.class” width = “100” height= “200” > </applet>
URL- Uniform resource locator which gives the address of the applet location.
https://gvsnarasimha.blogspot.com/ 1
Applets differ from Applications : Both applets and standalone applications are Java
applications but both differ in the following ways.
Applet Application
In applet we don’t write main( ) method Java application consist main( ) method
Applet begin its execution from init( ) method Java application begin its execution from main( )
method
Applets can access browser service It can access any data or software available on
the system.
Cannot access Native libraries(i.e. cannot access Supports Native Libraries
C , C++ Libraries )
Applets are run by using command Java Application run by using command
appletviewer <source file> java <Class Name>
Preparing to Write Applets: we need to check whether java is installed properly and ensure
that applet viewer or java enabled browser available.
We need to follow these steps to create and execute applets
1. Building applet code(.java file)
2. Creating an executable applet(.class file )
3. Designing web page using HTML tags
4. Writing <Applet> tag
5. Embedding <Applet> tag into the web page
6. Creating HTML file.
7. Testing the applet code.
Building Applet Code: to write applet code we need to import two classes Applet and
Graphics class from java library. Unlike other java applications applet begins its execution with
init( ) method, then start( ) and paint( ) methods will be executed. Thus every applet class
maintains life cycle of methods.
We use paint( ) method to display output on the screen
import java.awt.*;
import java.applet.*;
……………..
…………
public class appletclass extends Applet{
public void paint(Graphics g){ ………….
……………}
}
https://gvsnarasimha.blogspot.com/ 2
Applet program
import java.awt.*;
import java.applet.*;
public class FirstApplet extends Applet{
public void paint(Graphics g){
g.drawString("Hello Java",10,100);
}
}
/*<applet code = "FirstApplet.class" width = "200" height= "200" > </applet> */
Output:
Hello Java
https://gvsnarasimha.blogspot.com/ 3
Applet Life Cycle: Every java applet inherits set of default behavior from Applet class. As
result it undergoes several states they are
Born or Initialization state
Running State
Idle State
Dead or destroyed state
Initialization State: Applet enters the 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:
Create objects needed by the Applet
Set up initial values
Load images or fonts
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 }
https://gvsnarasimha.blogspot.com/ 4
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(), start() method can 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 current running applet. We can
also 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 display state will perform output operations on the screen.we need to
override paint( ) method.
https://gvsnarasimha.blogspot.com/ 5
Applet tag: In a HTML file we write <applet> tag as part of body section. an <applet> tag
specify three things.
1. Name of the applet
2. Width of the applet
3. Height of the applet
Syntax:
<APPLET
[CODEBASE= codebaseURL]
CODE = AppletFileName.class
[ ALT = alternate_text ]
[ NAME = applet_instance_name ]
WIDTH = pixels
HEIGHT = pixels
[ ALIGN=alignment]
[ VSPACE = pixels]
[ HSPACE = pixels]
>
[ < PARAM NAME = name1 value = value1> ]
[ < PARAM NAME = name2 value = value2> ]
………………
………………
</APPLET>
Attribute Meaning
HEIGHT = pixels
https://gvsnarasimha.blogspot.com/ 6