Introducing Java Applets
 
Published: 03 Jul 2007
Abstract
This article examines Java Applets with the help of examples.
by Debjani Mallick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 40032/ 54

Overview

An applet is a special kind of Java program which is used for internet programming. The applets reside in a server and they can be downloaded from the server to the client machine and executed on client machine provided that the browser should be enabled with Java technology. Applets are basically of two types.

Local Applet – These are applets which are created for standalone PC's where the same PC is distinguished as server and client. Hence, to execute a local applet there is no need of internet connection.

Remote Applet – The applets which are developed for an environment where the server and the client machine are physically apart from each other are called remote applets.

In order to locate and load the remote applet we must know the applet address on the web. This address is known as the URL. The difference between a Java applet and a Java application is that a Java application runs standalone, i.e. outside the browser while an applet runs in the context of a web browser, embedded within an HTML page.

Need of Applets

Web pages are basically of two types – static and dynamic web pages. Applets are basically used to create dynamic pages. Using HTML we can create only static pages, not dynamic pages. So Java using applet is used to create dynamic pages. There are two types of classes for applet – Applet class and Graphic class. The difference of an applet program from a normal program is that it does not contain the main function (though it can contain main function). So it has no entry point. The program starts executing from some functions present in Applet class. Secondly, an applet program cannot be executed independently as done in case of normal application programs. They must be executed with the help of either applet viewer or with the help of any web browser. If using an applet viewer to run a program, we just need to write the following syntax.

Listing 1

appletviewer classname

When we want to execute an applet program with the help of a web browser, we need to write an HTML file. This file must contain an applet tag. An example of such a file is shown below.

Listing 2

<html>
<head>
<title>
A Simple Example of Applet
</title>
<body>
<applet code ="filename.class" width =“100px” height =“100px”></applet>
</body>
</html>
Creating an applet program

Applet is a window based application. We need to import two packages for writing an applet program – java.lang.awt.* and java.lang.applet.*. The java.lang.awt.* package provides a set of classes and using those classes we can create a window component and it also contains a graphic class; the java.lang.applet.* package is used as we are using an applet class which is present in applet package.

Listing 3

import java.lang.awt.*;
import java.lang.applet.*;
public class classname extends Applet
{
  public void paint(Graphics g)
  {
    g.drawString(String,height,width);
  }
}

Public keyword is used because we run an applet program in a media different from that in which we write it. Paint() method used in the above example calls drawString() function which is a function of Graphics class. Applet class contains certain functions like init(), start(), stop(), paint(), destroy(), etc. which are usually used in programs.

init() method – Whenever an applet is loaded into a web page this method is automatically called. This method provides an appropriate place for initialization of the variables required and other set up operations such as setting of background color, defining the applet’s layout, parsing parameters, etc.

start() method – After the initialization is completed, start() method is called. The difference between this method and the init() method is that init() method is called only when the page is loaded, whereas start() method is called every time the page is loaded and every time the page is restarted. That is, if a user navigated from one page to another page and then he returns to the original page, start() method is called again but the init() method is not called again. This method is mainly used while implementing threads in Java.

stop() method – This method is called whenever the user navigates from one page to another page. It is always advisable to use stop() method if any type of animation or memory consuming activities is being performed in order to avoid wasting of system resources.

destroy() method – This method is called immediately before the applet exits. The exiting operation is caused by either the applet operation requesting for the exit or may be the web browser is shut down. When it is called it directs the applet to free up all the system resources being used by it.

paint() method – As mentioned earlier, this method is used to draw items.

Applet Life Cycle

Every Applet has:

Born state

Running state

Idle state (may or may not)

Dead state

Born State – When the class is executed the init function is automatically invoked. At this stage an applet is said to be born. This state is also called initialization state. During the applet life cycle, the born state occurs only once. The init function is defined within an applet. However, it can be overridden by the program. This function is used to initialize instance members, to load the required fonts, to initialize objects, to load pictures, to load back colors, etc.

Running State – After initialization, this state will automatically occur by invoking the start method of applet class which again calls the run method and which calls the paint method. The running state also occurs from idle state when the applet is reloaded.

Idle State – The idle state will make the execution of the applet to be halted temporarily. Applet moves to this state when the currently executed applet is minimized or when the user switches over to another page. At this point the stop method is invoked. From the idle state the applet can move to the running state.

Figure 1

Dead State – When the applet programs terminate, the destroy function is invoked which makes an applet to be in dead state.

Examples of applet programming:

Listing 4 – For printing a message

import java.lang.awt.*;
import java.lang.applet.*;
public class message extends Applet
{
    String msg;
    void init()
    {
        msg = “Welcome to applet programming”;
    }
    public void paint(Graphics g)
    {
        g.drawString(msg,100,400);
    }
}

Listing 5 - For printing the sum of two values

import java.lang.awt.*;
import java.lang.applet.*;
public class message extends Applet
{
    int x,y,z;
    String sum;
    void init()
    {
        x = 10;
        y = 20;
        z = x + y;
        sum = “The sum is” +String.valueOf(z);
    }
    public void paint(Graphics g)
    {
        g.drawString(sum,100,400);
    }
}

Listing 6 - For drawing a human face (eyes – black, ears – pink, face – yellow, lips – red)

import java.lang.awt.*;
import java.lang.applet.*;
public class face extends Applet
{
    public void paint(Graphics g)
    {
        g.setColor(Color.yellow);
        g.fillOval(150,150,100,100);
        g.setColor(Color.black);
        g.fillOval(170,170,15,15);
        g.fillOval(215,170,15,15);
        g.drawLine(200,190,200,210);
        g.setColor(Color.red);
        g.fillOval(190,220,20,10);
        g.setColor(Color.pink);
        g.fillOval(135,185,15,25);
        g.fillOval(250,185,15,25);
    }
}

Since everything has its brighter as well as darker side, applets also have their own limitations.

Applets do not use the main method for initializing the execution of the code.

Applets cannot be run independently. They are run inside a web page using HTML tag.

Applets are restricted from using library files.

Conclusion

I hope you enjoyed the ride with me on working with applets. In my next article I will discuss some more conceptual basics in Java.

By Debjani Mallick

www.mindfiresolutions.com



User Comments

Title: asd   
Name: asd
Date: 2012-11-29 7:57:45 AM
Comment:
asd
Title: Email Id Form in Java   
Name: Siva
Date: 2012-08-17 11:24:34 AM
Comment:
I want to know how to create email id form in java.
plz anybody known it plz upload it...
Thanks...
Title: applet   
Name: mayuri
Date: 2011-03-14 1:20:41 PM
Comment:
Its very helpfull to me,thanks
Title: applet   
Name: vida
Date: 2011-03-11 4:27:51 AM
Comment:
It was nice article & Very helpfull. Thank you.
Title: applet help   
Name: sam
Date: 2010-08-25 10:42:37 AM
Comment:
Thank you for the helpful and concise article. This was a good introduction to applets.
Title: Applets   
Name: veena
Date: 2010-04-19 6:40:44 AM
Comment:
easy understanding for the beginners
Title: Applets with asp.net   
Name: java
Date: 2010-02-12 12:47:23 PM
Comment:
See this article for info on how to integrate Java applets with asp.net
http://java.sun.com/developer/technicalArticles/appletAspIntegration/index.html
Title: query related to applets   
Name: mayank
Date: 2009-10-14 2:35:43 AM
Comment:
i m learning applet,can you please explain me how to write code for event handling in applets.if you know the answer then please mail to my email id mayank.mathur06@gmail.com.

Thank you.
Title: heyyyy   
Name: swathi
Date: 2009-08-10 3:47:13 AM
Comment:
its really very nice
Title: Applets   
Name: Anu
Date: 2009-07-23 1:56:43 PM
Comment:
It was a good article....you are doing a good job. Keeps on sharing your knowledge with others......thanks.
Title: Applets   
Name: Priya
Date: 2009-07-14 7:48:50 AM
Comment:
Hello,
awesome job!
Iam a learning applets and standing at initial stages.This article renders excellent learning resources with lots of examples written in short and sweet mannner that provide quick learning covering almost alll topics in short span of time.

Thanks and keep up the good work....
Title: Applets   
Name: Sutapa
Date: 2008-06-18 2:02:46 PM
Comment:
Really good article for beginners.....
Title: human face   
Name: ridima
Date: 2008-05-15 10:27:52 AM
Comment:
what if i have to run the human face applet from command prompt
it shows me an error that no main found
Title: java applets   
Name: kunwarjeet singh
Date: 2007-08-15 11:25:33 PM
Comment:
Too good for beginners.
Title: java applets   
Name: venkata subbaiah
Date: 2007-08-13 3:43:13 AM
Comment:
emerging one for quick learning
Title: Fabulous   
Name: Nikita
Date: 2007-07-22 9:06:43 AM
Comment:
Go on
Title: Too good   
Name: Fred
Date: 2007-07-18 11:53:32 AM
Comment:
Its a nice article for beginners
Title: good   
Name: Rishi
Date: 2007-07-13 1:10:32 PM
Comment:
A very good article
Title: Good   
Name: Chandrakanta Kar
Date: 2007-07-13 7:57:18 AM
Comment:
It is fundamental level article, any way nice
Title: abhi   
Name: gr8
Date: 2007-07-06 12:53:16 PM
Comment:
keep up the good work!!!!
Title: something really gooe   
Name: Surya
Date: 2007-07-06 12:18:03 PM
Comment:
Its really good
Title: Good one   
Name: sdwsadxa
Date: 2007-07-04 11:35:48 AM
Comment:
Keep it up
Title: Nice   
Name: Tim
Date: 2007-07-04 11:35:05 AM
Comment:
Nice job
Title: Good work   
Name: Sid
Date: 2007-07-04 11:34:34 AM
Comment:
The article is worth reading.
Title: Good   
Name: Nash
Date: 2007-07-03 3:21:39 AM
Comment:
Really helpful...good job

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-18 9:12:20 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search