6 Java
6 Java
6 Java
Java Applets
05/15/2024
What is Applet??
☞ Applets are Java programs (usually small) that generally run within web
browsers.
☞ They are usually incorporated in an HTML page.
☞ A web browser downloads the class file of the applet together with the
other parts of the web page from the web server.
☞ If the browser has a suitable Java Virtual Machine (JVM), it executes the
applet in the client computer.
☞ Since, applets run within web browsers in the client computer, they are
said to be client-side Java technology.
☞ Usually, applets are downloaded from a remote web server
05/15/2024
Cont…
• Consequently, they should be smaller in size.
• Larger applets have longer download time giving an unpleasant experience to the
visitors.
• Note that applets work for the local files also. So, we need not always deploy html files
containing applets in a web server.
• The html documents containing applets may be opened directly in a Java-enabled
browser.
• They work in the same way as they do upon downloading from a remote server.
05/15/2024
Cont..
o Development of an applet consists of two basic steps:
i. Writing the Java class and
ii. Incorporating it in an html document.
o An applet (a Java class) must be created by extending the
java.applet.Applet class which provides interfaces between the web
browser and the applet.
o If you want to use swing components, you should create your applets by
extending this javax.swing.JApplet class instead of Applet class.
05/15/2024
Cont..
☞ Note that the class, javax.swing.JApplet is a subclass of the Applet
class.
05/15/2024
Cont...
An applet, from its birth to death, goes through a number of states:
Instantiated or born
Running
Idle or Stopped
Dead or destroyed
The duration when an applet remains alive is called life cycle of the applet.
The class Applet provides a framework where your applets can run.
It has a set of methods to control and supervise the smooth execution of applets
These methods (also called life cycle methods) are called in a specific order during an
applet’s entire life cycle.
An applet usually overrides these methods to do a designated task.
05/15/2024
Cont..
Figure 7.3: Life cycle of an applet
• Even though, life cycle methods are called automatically by the browser, we should have
sound knowledge about when they are called and what we can do with these methods. The
following is a brief description of these life cycle methods.
05/15/2024
init()
This is the first method that gets called only once when an instance of
the applet is created and loaded by a web browser.
Applets do not have any constructor. Since this method is called
before all other methods, it can be used as a constructor.
Usually, the piece of code that we write in the constructor goes here.
For example, initializing variables, setting background and
foreground color may be done here.
05/15/2024
start()
05/15/2024
paint()
05/15/2024
Cont..
• This method takes a single argument of the type Graphics, which encapsulates
the graphic window the applet is contained in.
• It has numerous useful methods that are used to display AWT components
such as graphic elements (e.g. lines, ovals, rectangles, etc.), images, strings,
etc. on the window.
• This is the method where we can write our code of what we expect from the
applet such as animation etc.
05/15/2024
stop()
05/15/2024
destroy()
❡ This method is called only once when the browser window containing the
applet is closed (exited).
❡ This method makes the applet dead (i.e., the applet is not available any
more).
❡ Typically, you need not override this method. If a browser window
containing a running applet is closed, the stop() method is called first,
which calls destroy() method after performing all the necessary tasks to
shut down the applet.
❡ However, the method is still available to release other resources.
05/15/2024
WRITING AN APPLET
• Let us now write our first applet called HelloWorld which will display a
simple message “Hello World!” on the screen.
The following is the source code of the HelloWorld applet stored in the file
HelloWorld.java.
//HelloWorld.java
public class HelloWorld extends java.applet.Applet
{
public void paint(java.awt.Graphics g)
{
g.drawString("Hello World!", 20, 20);
}
}
05/15/2024
GENERATING CLASS FILE
The following section describes how to embed an applet within an html file.
05/15/2024
The Applet Tag
Two HTML tags are used to work with applets: <applet> and <param>.
An applet is typically embedded in an HTML document using the <applet> tag.
The <param> tag is used to pass parameters to an applet.
Its syntax is given below:
<param name = parametername value =
stringvalue />
The <applet> tag
It instructs the browser that an applet has to be loaded and executed using the Java
Runtime Environment (JRE).
It takes three mandatory attributes: code, width, and height.
The code attribute specifies the class file for this applet to be instantiated.
The width and height attributes indicate the width and height, respectively, of the
applet window to be created.
05/15/2024
Cont…
• For our HelloWorld applet, we create a simple HTML file, HelloWorld.html,
as follows:
<!-- HelloWorld.html -->
<html>
<head>
<title>Applet Demo</title>
</head>
<body>
<applet code="HelloWorld" height=50 width=150 >
</applet>
</body>
</html>
• The you use the application appletviewer, which comes with JDK, as follows.
appletviewer HelloWorld.html
05/15/2024
Cont…
• Sometimes, for quick development and to avoid creating a separate
html file, the applet is embedded in the applet source file (.java file).
For example, we can embed our HelloWorld applet in the
HelloWorld.java file as follows:
/*
<applet code="HelloWorld" height=50 width=300>
</applet>
*/
public class HelloWorld extends java.applet.Applet
{
public void paint(java.awt.Graphics g)
{
g.drawString("Hello World!", 20, 20);
}
05/15/2024
}
Cont…
• We can now pass this document to the appletviewer program.
• Since, the <applet> tag (which is not a valid Java code) is inserted in the
Java source file, which is compiled using javac, the tag is written as
comments to avoid compilation error.
• As mentioned earlier, the appletviewer program looks at only <applet>
tag ignoring everything else.
• Consequently, it is expected to display our applet in its windows.
The following command may be used to view the applet.
appletviewer HelloWorld.java
05/15/2024