Java Applet Programming

Home   »    Java   »    Java Applet Programming


Applets are small applications that are accessed from an internet server, transported over the Internet, automatically installed and run as part of a web document. After an applet arrives on the client it has limited access to resources, so that it can produce an arbitrary multimedia user interface and run complex computations without introducing the role of viruses or breaching data integrity.

Contents



Local and Remote Applets

Java provides us the facility for both creating CUI and GUI Programs All the Previous Topics are Related with the CUI But Applets Provides the ability to user for creating Graphical Programs Like Creating Buttons, Creating Events such that Executing the Code when a user Clicks on Button Etc. There are two type of Applets Like Stand Alone or either Local Applets The Applets those are Created on Client Machine are Called as Local Applets and the Applets Those are located at Remote Computer but we are using them then they are Called as Remote Applets . Applets are used for creating Graphical Programs.




How Applets Differ from Applications

There are significant differences between applets and stand alone java applications. Applets are not full featured application programs. Applets are usually written to accomplish a small task or a component of a task. They are designed for use on the Internet, they impose certain limitations and restrictions in their design.

  1. main() method is not used in any Applet as it is not required for initiation the execution of the code, where as , Applet when loaded, automatically call certain methods of Applet class to start and execute the applet code.
  2. Applets are run from inside a web page using a special feature known as HTML tag. Where as application run stand-alone and independently on web.
  3. Files that are located in the local computer cannot be read or write by the applets.
  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++.




Preparing to Write Applets

Until now we have been creating simple Java application program with a single main() method that created objects, set instance variables and ran methods. To write any applet, we will need to know:

  1. When to use applets.
  2. How an applet works,
  3. What sort of features an applet has, and
  4. Where to start, when we first create our own applet.

The following are the steps that are involved in developing and testing and applet.
  1. Buliding an applet code(.java file)
  2. Creating an executable applet(.class file)
  3. Designing a web page using HTML
  4. Preparing <Applet Tag>
  5. Incorporating <Applet> tag into the web page.
  6. Creating HTMl file.
  7. Testing the applet code.




Buliding Applet Code

To building the applet code two classes of java library are essential namely Applet and Graphics. The Applet class is contained in java.applet package provides life and beehaviour to the applet through its methods such as int(), start() and paint(). Unlike with 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.

To display the result of the applet code, the paint() method of the Applet class is called up. The output may be test, graphics, or sound. The syntax of paint() method which requires a Graphic 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 contain the Graphic class. All output operations of an applet are performed using the methods defined in the graphics class.
The general format of applet code is as following

import java.awt.*;
import java.applet.*;
___________________
___________________
public class applet classname extends Applet
{
  .................................
  ................................statements
  ................................
  ................................
  public void paint(Graphics g)
  {
    ..........................
    ..........................//Applet operations code
    .........................
  }
  ......................
  .....................
}




Applet Life Cycle

An applet is a window based event driven program and it waits until an event occurs. The AWT notifies the applet about an event by calling an event handler that has been provided by applet. Applet should not enter a "mode" of operation in which it maintains control for an extended period. In these situations you must start an additional thread of execution.

The user initiates interaction with an applet. For example, when the user clicks a mouse inside the applet's window, a mouse-clicked event is generated. Applets can contain many controls such as push button check boxes. When the user interacts with one of these controls, an event is generated.



public class AppProgram extends Applet
{
  public void init()
  {
    //initializtion
  }
  public void start()
  {
    //start or resume execution
  }
  public void stop()
  {
    //suspend execution
  }
  public void destroy()
  {
    //perform shutdown activities
  }
  public void paint(Graphics g)
  {
    //redisplay contents of window
  }
}

The skeleton generates the blank applet window when viewed with an applet viewer. This skeleton does not do anything.


Initialization State
When the browser downloads an HTML page containing applets, it creates an instance for each of the Applet classes, using the no arg constructor. Applet must have a no argument constructor otherwise it cannot be loaded. Initialization can be done through init(). The init() method is the first method to be called. It is used to initialize the applet each time it is reloaded. Applets can be used for setting up an initial state, loading images or fonts, or setting parameters. For example:

public void init()
{
  //code here
}


Running State
Immediately after calling init(), the browser calls the start() method. start() is also called when user returns to an HTML page that contains the applet. So, it ht user leaves a web page and come back, the applet resumes execution at start(). So, when the applet called the start() Method it is called its running state. Staring can also occur if the applet is already in "stopped" (idle) state.

public void start()
{
____________________
____________________
(Action) ____________________
}


Idle or Stopped State
When we leave the page containing the currently running applet, then it stop running and becomes idle. We can also do so by calling stop() Method explicitly.

public void stop()
{
____________________
____________________
(Action) ____________________
}


Dead State
When an applet is completely removed from the Memory, it is called dead. This occurs automatically by invoking the destroy() method when we quit the browser Like initialization, dead state occurs only once in the applet's life cycle.

public void destroy()
{
____________________
____________________
(Action) ____________________
}


Display State
Painting is how an applet displays something on screen-be it text, a line, a colored background, or an image. The paint() method is used for displaying anything on the applet paint() method takes an argument, an instance of class graphics. The code given can be as follows:

public void paint(Graphics g)
{
}

where Graphics class contains the member functions that can be used to display the output to the browser.

text String, the drawString() method is used.
g.drawString(String s, int x, int y)

where x and y are the coordinate positions on the screen and s is the string to be output.




Creating an Executable Applet

Executable applet is nothing but the .class file of applet, which is obtained by compiling the source code of the applet. Compiling the applet is exactly the smae as compiling an application using following command.

javac appletname.java

The compiled output file called appletname.class should be placed in the smae directory as the source file.


Designing a Web Page

Java appleta are programs that reside on web page. A web page is basically made up of text and HTML tags that can be interpreted by a web browser or an applet viewer. Like Java source code, it can be prepared using any ASCII text editor. A web page is also called HTML page or HTML document. Web pages are stored using a file extension .html such as my Applet.html. Such files are referred to as HTML files. HTML files should be stored in the same directory as the compiled code of the applets.

A web page is marked by an opening HTML tag <HTML> and closing HTML tag </HTML> and is divided into the following three major parts:

1) Comment Section
2) Head Section
3) Body Section

Comment Section
This is very first section of any web page containing the comments about the web page functionality. It is important to include comments that tell us what is going on in the web page. A comment line begins with <! And ends with a > and the web browsers will ignore the tesxt enclosed between them. The comments are optional and can be included anywhere in the web page.

Head Section
This section contains title, heading and sub heading of the web page. The head section is defined with a starting <Head> tag and closing </Head> tag.

<Head>
<Title> Welcome to Java Applet </Title>
</Head>


Body Section
After the Head Section the body section comes. This is called as body section because the entire information and behaviour of the web page is contained in it, It defines what the data placed and where on to the screen. It describes the color, location, sound etc. of the data or information that is going to be placed in the web page.

<Body>
<Center>
<H1>
Welcome to Java >
</H1>
</Center>
<BR>
<APPLET - - ->
</APPLET>
</Body>




Applet Tag

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 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 this HTML file. It also specifies the display area for the applet output as 400 pixels width and 200 pixels height. We can make this display area appear in the center of screen by using the CENTER tags as showsn below:

<CENTER>
<Applet>
-------------
-------------
-------------
</Applet>
</CENTER>

The applet tag discussed above specified the three things:

1) Name of the applet
2) Width of the applet (in pixels)
3) Height of the applet (in pixels)




Adding Applet to HTML File

To execute an applet in a web browser, you need to write a short HTML text file that contains the appropriate APPLET tag. Here is the HTML file that executes SimpleApplet:

<Applet code = Hellojava.class width = 400 Height = 200 >
</Applet>

The width and height attributes specify the dimensions of the display area used by the applet. After you create this file, you can execute the HTML file called RunApp.html (say) on the command line.

c:\>appletviewer RunApp.html




Running the Applet

To execute an applet with an applet viewer, you may also execute the HTML file in which it is enclosed, eg.

c:\>appletviewer RunApp.html

Execute the applet the applet viewer, specifying the name of your applet's source file. The applet viewer will encounter the applet tage within the comment and execute your applet.




First Java Applet Program

The Following will be saved with named FirstJavaApplet.java:


After creating FirstJavaApplet.java file now you need create FirstJavaApplet.html file as shown below:

After this you can comiple your java applet program as shown below:

"Running Applet"
"Output of FirstJavaApplet.class"




Download Complete Program

  -  




More examples in Java Applet









<< Previous Topic
Next Topic >>




2 comments: