6 Java

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Chapter Six

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 Figure 7.1: Execution of an Applet


LIFE CYCLE

✦ Though applets are Java programs, they have certain


differences from Java application programs.
✦ Applets, unlike Java application programs, do not have any
main() method. Instead, they have an init() method that can be
used for a similar purpose.
✦ Applets do not have any constructor. [Doubt]
✦ The way instances of applets are created and run is completely
different from the way objects are created in Java application
programs.

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.

Figure 7.2: The Applet class hierarchy

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()

 This is the second method that gets called in the sequence


 Note that init() method merely creates an instance of applet which is
not yet active.
 The start() method, as the name implies, starts the execution of an
applet.
 It is also called every time an applet is restarted (revisited).
 Restarting an applet means the applet was running, but stopped for
some reason using stop() method earlier and needs to be started again.
 If you want to perform a task after initialization, override this
method.

05/15/2024
paint()

• This method is inherited from the java.awt.Container class.


• It is called immediately when the applet starts its execution.
• This method is also called when the output of an applet is redrawn.
There are several situations when an applet is redrawn.
❇ When the window containing the applet is minimized and restored
❇ When the applet’s container window is overwritten by another window and is
uncovered at some later point of time .
❇ When the window containing this applet is moved/dragged to another
location

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()

• The method stop() makes the applet inactive temporarily.


For example, when a visitor switches to another window, leaving the
current window containing the applet this method is called.
• However, the applet may come back to the running state again using
a call of start() method again.
• This way, an applet can switch between these two states (stopped
and running) any number of times in its life cycle.
• It is the right place to write code to cleanup resources that are not
necessary during the inactive period.

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

 To compile our HelloWorld applet, go to the directory containing the file


HelloWorld.java and use the following command:
 javac HelloWorld.java
 On successful compilation, it creates a class file named HelloWorld.class,
which contains the byte code for our HelloWorld applet.
RUNNING THE APPLET
• Running an applet is different from running a Java application program.
• To run an applet, its class file has to be embedded in an html file using
<applet> tag.

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

You might also like