top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java Applets

+1 vote
224 views

Java Applet

Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

Because Java applets run inside the Java browser, they have access to the same capabilities that the browser has: sophisticated graphics, drawing, and image processing packages; user interface elements; networking; and event handling.

Creating Applets

To create an applet, you have to create a subclass of the class Applet, in the java.applet package. The Applet class provides behavior to enable your applet not only to work within the browser itself, but also to take advantage of the capabilities of AWT to include UI elements, to handle mouse and keyword events, and to draw to the screen. Although your applet can have as many “helper” classes as it needs, it’s the main applet class that triggers the execution of the applet. That initial applet class always has a signature like this:

public class myClass extends java.applet.Applet  {

...

}

When Java encounters your applet in a Web page, it loads your initial applet class over the network, as well as any other helper classes that first class uses. Unlike with applications, where Java calls the main() method directly on your initial class, when your applet is loaded, Java creates an instance of that class, and all the system-based methods are sent to that instance. Different applets on the same page, or on different pages that use the same class, use different instances, so each one can behave differently from other applets running on the same system.

Lifecycle of Java Applet

  • Applet is initialized.
  • Applet is started.
  • Applet is painted.
  • Applet is stopped.
  • Applet is destroyed.

init − This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.

start − This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.

stop − This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.

destroy − This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.

paint − Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

posted Aug 24, 2017 by Ganesh S

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

Uses of Applets

Applets can be used for multiple purposes. Some of the uses of applets are:

  • Applets are used on Internet for creating dynamic web pages. There are two types of web pages: Static and Dynamic. Static web pages provide some information to the user but the user cannot interact with the web page other than viewing the information. Dynamic web pages interact with the user at the runtime. For example, a student can type his hall ticket number in a text field and click the retrieve button to get back his result from his University server. Applets are useful to provide such interaction with the user at runtime.
  • Another use of applets is for creating animation and games where the images can be displayed or moved giving a visual impression that they are alive.

In fact, applets are one of the main reasons for the popularity of java on Internet. In 1995, when javasoft people demonstrated some animation using an applet on HotJava browser, developers started believing that it is possible to perform animation in the browsers.

Now-a-days most of the websites on Internet are dependent on other software like PhotoShop, Flash, Dreamweaver, etc .  For creating, editing, and providing animation to the images. But for validating the form data, scripting languages like JavaScript and PHP are better. Hence the use of applets is diminishing rapidly.

<APPLET> tag

<APPLET> tag is useful to embed an  applet into an HTML page.  It has the following form:

<APPLET CODE = “name of the applet class file”

                                         CODEBASE=”path of the applet class file”

                                         HEIGHT= maximum width of applet in pixels

                                         ALIGN= alignment (LEFT, RIGHT, TOP, BOTTOM, MIDDLE)

                                         ALT= alternate text to be displayed>

                                        <PARAM NAME= parameter name VALUE= its value>

</APPLET>

The <PARAM> tag is useful to define a variable (parameter) and its value inside the HTML page which can be passed to the applet. The applet can access the parameter value using getParameter() method , as:

String value= getParameter(“Pname”);

Here, pname is the parameter name and its value is retrieved by the above method into String type variable: value.

A Simple Applet

Let us create an applet that displays ‘Hello applet’ in the applet frame. To display a message, we can take the help of paint() method of Component class of java.awt package. Remember all the methods of the applet and the applet class itself should be declared ‘public’, otherwise they are not available to the browser to execute.

Program 1: This is java program that creates an applet with yellow background color and a message ‘Hello Applets”.

// A simple applet

import java.awt.*;

import java.applet.*;

public class myapp extends Applet

{

// set a background color frame

    public void init()

                {

                         setBackground(Color.yellow);

                                                 

                }

                   //display message in applet window

                public void paint(Graphics g)

             {

                         g.drawString("Hello Applets!",50,100);

           }         

}             

Run: javac myapp.java

Now, MyApp.class is created. This byte code should be embedded into a HTML page using <APPLET> tag, as shown below:

<! myapp.htmlat embeds myapp applet>

<html>

<applet code="myapp.class" height=300 width=400>

</applet>

</html>

Save the above code with the name: myapp.html. This HTML page contains the applet which can be opened in the browser, or an applet  viewer supplied by the  Sun Microsystems Inc., can be used to test the applet. For this purpose, open any browser and in the browser’s address bar, type .html file name along with the directory path. The applet opens in the browser. Or, give the command at system prompt as:

Run: Appletviewer myapp.html

OUTPUT.

READ MORE
...