Applets in java

Applets in java

Applets

Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as a part of a web document.

After a user receives an applet, the applet can produce a graphical user interface.

Applets in Java are small and dynamic internet-based programs. Generally, the applet code is embedded within an HTML page. The applet codes are executed when the HTML pages get loaded into the Java-compatible web browsers. Applets are mainly downloaded on remote machines and used on the client side.

 

Example:

Below I have written a simple Java applet program which will simply display the welcome message.

EduApplet.java

import java.applet.Applet;

import java.awt.Graphics;

 //Extending the Applet class

public class EduApplet extends Applet{

public void paint(Graphics g){

g.drawString("Welcome",150,150);

}

}

Executing Applets in Java

By now I have demonstrated how to create an applet, executing applets is a bit different process. Since applets are net based applications they need a special environment to execute. Java provides two standard ways to execute an applet:

1.      Using a Java-Enabled Web Browser

If you are trying to execute your Applet in this way, first you need to compile your Java Applet file. Once done, you have to create a separate HTML file and add the applet code within it with the reference to your .class file within it. Now you can click the HTML file to launch the applet in the browser. Below I have given the code required to create the HTML file:

appletDemo.html

<html>

<body>

<applet code="EduApplet.class" width="300" height="300">

</applet>

</body>

</html>

2.      Using appletviewer

In order to execute a Java Applet in this way, all you need to do is, instead of creating a separate file for HTML code, you can directly add comment at the beginning of your Java source code file indicating the presence of APPLET tag within. This been done, now you can execute your applet just by starting the Java Applet Viewer which comes by default with JRE.

EduApplet.java

import java.applet.Applet;

import java.awt.Graphics;

 /*

<applet code="EduApplet" width=200 height=60>

</applet>

*/

 public class EduApplet extends Applet{

public void paint(Graphics g){

g.drawString("Welcome To Edureka's Applet Tutorial",150,150);

}

}

 

Applet Life Cycle (Skeleton)

There are 4 main methods which are mandatory for any Java Applet to override. Let me brief you about each of these methods one by one.

1.      public void init():  This is the very first method to be invoked during the life cycle of an applet. In this method, the variable that will be used further in the applet is initialized. One thing you must note here is that this method can be invoked only once per applet life cycle.

2.      public void start(): This is the second method that is invoked just after the init() method is called by the browser. Each time a user revisits the web page containing the applet, start() method is invoked and the applet is started.

3.      public void stop(): This method is invoked whenever a user leaves the web page containing applet. In other words, the stop() method is used to suspend the threads which are not required when the applet is in the background or is not visible on the screen. These can be easily resumed using the start() method.

4.      public void destroy(): Finally, we have the destroy() method which is invoked in order to completely remove an applet from the memory. This method is invoked only once per applet life cycle and all the engaged resources must be freed up before this method is called.

One more method that is mostly used along with the above four is paint().

public void paint(Graphics g): This method is invoked whenever an applet needs to be redrawn or repainted in the browser, irrespective of the cause. The paint() method takes one Graphic object as a parameter that contains the graphics context in which the applet is being executed. Also, this method is invoked each time output is expected from the applet.

 

Parameter in Applet

Syntax:

<PARAM name = ……… Value = “………” >

init() method is used to get hold of the parameters which is defined in the <PARAM> tags. And getParameter() method is used for getting the parameters.

example:

param.java

 

import java.applet.*;

import java.awt.*;

public class param extends Applet

{

  String str;

  public void init()

  {

    str=getParameter("pname"); 

    if (str == null)

    str = "Welcome to studytonight.com";

    str = "Hello " + str;

  }  

    public void paint(Graphics g)

    {

      g.drawString(str, 200, 200);

    }

}

  param.html

<html>

<applet code=param.class height=300 width=300>

<param Name="pname" value="Welcome to applet">

</applet>

</html>

 

Displaying Image in Applet

Applet is mostly used in games and animation. For this purpose image is required to be displayed. The java.awt.Graphics class provide a method drawImage() to display the image.

Syntax of drawImage() method:

public abstract boolean drawImage(Image img, int x, int y, ImageObserver observer);

Example of displaying image in applet:

import java.awt.*; 

import java.applet.*; 

  public class DisplayImage extends Applet { 

    Image picture; 

    public void init() { 

    picture = getImage(getDocumentBase(),"sonoo.jpg"); 

  } 

public void paint(Graphics g) { 

    g.drawImage(picture, 30,30, this); 

  } 

  } 

In the above example, drawImage() method of Graphics class is used to display the image. The 4th argument of drawImage() method of is ImageObserver object.

myapplet.html

<html> 

<body> 

<applet code="DisplayImage.class" width="300" height="300"> 

</applet> 

</body> 

</html> 


Comments

Popular posts from this blog

Accounting Multiple Choice Questions with answers | Download PDF for MCQs

Difference between Data Mining and Knowledge Discovery (KDD)