Skip to main content

What is Applet ?

***What is an Applet?


An Applet is a program that can be
referenced by an HTML source code of a
web page.


It is dynamically downloaded from a web
server to a browser and it is executed in the
environment provided by the browser.



  • Applet Viewer can also be used to execute  the applet.


***FirstApplet.java

import java.applet.Applet;
import java.awt.Graphics;
/*<applet code=“FirstApplet” width=200 height=200>
</applet>*/
public class FirstApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString(“Hello World”,20,100);
}
}


*** In the above example,

The first two line is importing the java.applet and
java.awt.Graphics package.

The third line is the comment entry which is used only for
applet viewer to execute the applet using the HTML source
code.

The next line declares the FirstApplet class which extends
Applet class. Note that each applet created by user must
extend the Applet class.

There is a paint() method defined which accepts the object of
Graphics class. This method is invoked automatically
whenever the applet is displayed.

The drawString() method is defined in the class Graphics and
it takes three arguments, message and x and y co-ordinates.
Appletviewer is used to execute the applet.



***LIFE CYCLE OF APPLET

Normally, any program starts execution
from the main() method of a class.

An apple executes in an environment
provided by the browser or an appletviewer.

There are total four methods that are called
during the life cycle of an applet. They are:
init(), start(), stop() and destroy().

All these methods are defined in the
java.applet.Applet class.

The init() method is called when the applet
begins execution. The code which is to be
executed only once is placed in this method.


The start() method is executed after the
init() method completes execution. This
method is also called when an appletviewer
or the browser resumes the execution of the
applet.

The stop() method is called by the applet
viewer or web browser to suspend
execution of an applet. Therefore, the
start() and stop() methods may be called
multiple time during the life cycle of the
applet.

The stop() method is also called when the
browser calls another web page.

Finally, the destroy() method is called by the
applet viewer or web browser before the
applet is terminated.

The stop() method is invoked before the
destroy() method.



EXAMPLE :

import java.applet.Applet;
import java.awt.Graphics;
/*<applet code="AppletLifeCycle" width=200
height=200></applet>*/

public class AppletLifeCycle extends Applet
{
String str="";
public void init()
{
str+="Init:";
}
public void start()
{
str+="Start:";
}
public void stop()
{
str+="Stop";
}
public void destroy()
{
System.out.println("Destroy");
}
public void paint(Graphics g)
{
g.drawString(str,20,20);
}
}


***GRAPHICS CLASS
A graphics object encapsulates a set of
methods that can perform graphics output.


It allows to draw lines, ovals, rectangles,
strings, images, characters and arcs.




Comments

Popular posts from this blog

16 to 20 Java questions

16. How do you set security in applets? using setSecurityManager() method 17. What is a layout manager and what are different types of layout managers available in java AWT? A layout manager is an object that is used to organize components in a container. The different layouts are available are FlowLayout, BorderLayout, CardLayout, GridLayout and GridBagLayout 18. What is JDBC? JDBC is a set of Java API for executing SQL statements. This API consists of a set of classes and interfaces to enable programs to write pure Java Database applications. 19. What are drivers available? a) JDBC-ODBC Bridge driver b) Native API Partly-Java driver c) JDBC-Net Pure Java driver d) Native-Protocol Pure Java driver 20. What is stored procedure? Stored procedure is a group of SQL statements that forms a logical unit and performs a particular task. Stored Procedures are used to encapsulate a set of operations or queries to execute on database. Stored procedures can be compiled and...

Latest Java 11 to 15

11. What are the states associated in the thread? Thread contains ready, running, waiting and dead states. 12. What is synchronization? Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time. 13. What is deadlock? When two threads are waiting each other and can’t precede the program is said to be deadlock. 14. What is an applet? Applet is a dynamic and interactive program that runs inside a web page displayed by a java capable browser 15. What is the lifecycle of an applet? init() method – Can be called when an applet is first loaded start() method – Can be called each time an applet is started. paint() method – Can be called when the applet is minimized or maximized. stop() method – Can be used when the browser moves off the applet’s page. destroy() method – Can be called when the browser is finished with the applet.

What is Servet ?

A servlet is a java class that extends an application hosted on a web server. Handles the HTTP request-response process (for our purposes) Often thought of as an applet that runs on a server. Provides a component base architecture for web development, using the Java Platform The foundation for Java Server Pages (JSP). Alternative to CGI scripting and platform specific server side applications.