Wednesday 26 February 2020

Java applet | How to create applet in java | life cyle of applet - 0nline help

We are going to discuss java applet. It’s advantages and disadvantages, the life cycle of an applet and sample program, etc.

java applet

What is Java applet:

The applet is small applications that are accessed on an internet server, transported over the internet, automatically installed and run as part of a web document.

An applet is developed for internet applications. Applets are embedded in HTML documents. Applets program can be run using a web browser or applet viewer.

The applet viewer tool is one of the tools included in the JDK(Java development kit) applet viewer. For example, when you use Java to enhance a web page the Java code is embedded within HTML code. It is called an applet.

Advantages and disadvantages of the applet:

Advantages of the applet:

Following are the advantages of an applet:

  • The most important feature of an applet is, it is truly platform-independent so there is no need of making any changes in the code for the different platform i.e. it is simple to make it works on Linux, Windows and Mac OS to make it cross-platform.
  • The same applet can work on all installed versions of Java at the same time, rather than just the latest plugin version only.
  •  It can move the work from the server to the client, making a web solution more scalable with users/clients.
  • The applet naturally supports the changing user state like figure position on the chessboard.
  • An applet improves with use: after the first applet is run the JVM is already running and starts quickly.
  • It can be used to provide dynamic user interfaces and a variety of graphical effects for web pages.

 Disadvantages of applets:

 The following are the disadvantages of an applet:

  • Applets cannot run any local executable programs.
  •  It can't talk with any host other than the originating server.
  •  Applets cannot read or write to the local computer file system.
  •  It cannot find any information about the local computer.
  •  All Java created a popup window carrying a warning message.
  •  Stability depends on the stability of clients with the server.
  •  Performance directly depends on the client's machine.

Also read: Multithreading in java

How to create java applet:

The creation of an applet is present here because the applet is not structured in the same way as the Java program has been used. As you will see, applet differs from application in several key areas:

Example:
import java.awt.*;
import java.applet.*;
Public class simpleapplet extends Applet
{
Public void paint(Graphics g)
{
g.drawString(“a simple applet”,20,20);
}
}

This applet begins with two import statements. The first import the Abstract window toolkit(AWT) classes. The applet interacts with the user through the AWT. The AWT supports Windows-based, graphical interface.

 The AWT is quite large and sophisticated. So, the applet makes very limited use of the AWT.

The second statement imports the applet package which contains the class applet. Every applet that you create must be a subclass of the applet.

The next line in the program declared the class simpleapplet. This class must be declared as public because it will be accessed outside the program.

Also read: Static and fixed method in java

Inside simplapplet, paint() is declared. This method is defined by the AWT and must be overridden by the applet.

paint() is called each time that is the applet must be redisplayed its output.

For example, the window in which are the applet is running can be minimized and restored. paint() is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint() is called.

The paint() method has one parameter of type Graphics. This parameter contains the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever the output of the applet is required.

Inside paint() is called to the drawString() which is a member of the Graphics class. This method outputs a string beginning at the specified X, Y location. It has the following general form:

Void drawString(string message,int x,int y);

Here the message is the string to be output beginning at the X, Y. In a java window, the upper-left corner is the location 0,0. The call to drawString() in the applet causes the message “a simple applet” to be displayed beginning at the location 20,20.

Notice:
The applet doesn't have a main() method. It does not begin execution at the main().In fact the most applets don't ever have main() method. An applet begins execution when the name of its class is passed to an applet viewer or to a net Browser.

After you enter the source code for simpleapplet, compile in the same way that you have been compiling a Java program.

However, running a simpleapplet involves a different process. There are two ways in which the applet run. They are:

-  We can execute the applet within a java compatible web browser such as Netscape navigator.

-  Using the applet viewer, such as the standard JDK tool, applet viewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.

Also read: Java Synchronization

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 a simpleapplet.

<applet code=”simpleapplet” width=200 height=60>

</applet>

Save the file(as may be simpleapplet with .html extension). To execute a simpleapplet with an applet viewer, you may also execute the HTML file. Type the following command at the DOS prompt:

C:\>appletviewer simpleapplet.html

However, a more convenient method that you can use to speed up testing simply includes the HTML code at the head of your Java source code file that contains the Applet tag.

By doing this, your code is documented with a prototype of the necessary HTML statement and you can test your compiled applet merely by starting the applet viewer with your Java source code file. If you use this method the simple applet source file look like this:

Example:

Import java.awt.*;
Import java.applet.*;
/*
<applet code=”simpleapplet” width=200 height=60>
</applet>
*/
Public class simpleapplet extends Applet
{
Public void paint(Graphics g)
{
g.drawString(“a simle applet”,20,20);
}
}

Java applet life cycle:

The class applet has four methods that constitute the applet life cycle. They are: init(0,start(),stop(), and destroy(). These are defined by the applet class java.applet.*;
1.Public void init(0
2.Public void start()
3.Public void stop()
4.Public void destroy()

Graphical representation of the life cycle of a java applet:


java applet


Another method paint() is inherited by the applet class from the component class.

1.init()- The init() method is called only when the applet begins execution.

2.start()- The start() is executed after the init() method completes execution. This method is called by the applet viewer of the web browser to resume the execution of the applet.

3.stop()- The method stop() is called by the applet viewer or the web browser to suspend the execution of an applet.

4.destroy()- The method destroy() is called before the applet is terminated by the applet viewer or the web browser.

Sample program to demonstrate the applet life cycle:

import java.awt.*;
import java.applet.*;
/*<applet code=”applettest” width=200 height=100>
</applet>
*/
public class applettest extends Applet
{
public void init()
{
System.out.println(“applet initialized…”);
setBackground(Color.cyan);
}
public void start()
{
System.out.println(“applet is started…”);
}
public void stop()
{
System.out.println(“applet is stopped…”);
}
public void destroy()
{
System.out.println(“applet is destroyed…”);
}
Public void paint(Graphics g)
{
g.drawString(“applet text”,200,400);
showStatus(“this is shown in the status”);
}
}

Applet tags in HTML:

The applet tag is used to start an applet from both HTML documents and the applet viewer. An applet viewer will execute each applet tag that is fine in a separate window, while web browsers like Netscape Navigator, Internet Explorer and hot java will allow many applets on a single page.

The <applet> tag included in the body section of the HTML file supplies the name of the applet to be loaded and tells the browser how much space the applet requires.

The <applet> tag is used to incorporate an applet into a web page. The syntax of the <applet> is

<applet code=”filename.class” width=250 height=200>

</applet>

General structure of <applet> tag is:

<applet 
code=appletname.class
alt=alternate message
width=pixels
height=pixels
align=value
vspace=value
hspace=value>
</applet>

Also read: Thread in java

Here the attributes of an <applet> are

code: Name of the applet class file
Alt: alternate message
width: width of the applet window
height: height of the applet window
align: it aligns the applet window. Commonly used values are left,right,top,bottom,middle.
vspace: specifies the vertical blank space
hspace: specifies the horizontal blank space

Passing arguments to the applet:

Java allows users to pass user-defined parameters to applets with the help of <param> tags. Every applet has a built-in way of reading parameters from the <applet> tag.

The <param> tag is used within the <applet> tag pair t pass input as a parameter to an applet.

The <param> tag has a name attribute which defines the name of the parameter and a value attribute which specifies the value of the parameter.


The syntax of the <param> tag is:

<applet>
<param name=parameter1_name value=parameter1_value>
<param name=parameter2_name value=parameter2_value>
.
.
.
<param name=parametern_name value=parametern_value>
</applet>

For example, consider the following statement to set the text attribute to the applet:

<applet>
<param name=text value=This is an example of parameter>
</applet>

Accessing parameter:

Note that the <param> tags must be included between the <applet> and </applet> tags. The init() method in the applet retrieves user-defined value of the parameters defined in the <param> tag by using the getParameter() method.

This method accepts one string argument that holds the name of the parameter and returns a string that contains the value of that parameter.

The following program demonstrates the <param> tag i.e. passing parameters to an applet:

import.java.awt.*;
Import java.applet.*;
/*
<applet code=” passing.class” width=200 height=200>
<param name=”college” value=”neha”>
</applet>
*/
Public class passing extends applet
{
String str=” hi”;
public void init()
{
str+=getParameter(“college”);
}
public void paint(Graphics g)
{
g.drawString(str,50,50);
}
}

Also read: DeadLock in java

I hope you guys understand what is java applet? how can we create it and also get to know the life cycle of an applet? If you have any doubt or information regarding this, then please do comment.

No comments:

Post a Comment