Applet Programming: Unit:5
Applet Programming: Unit:5
Unit:5
Difference between Application and Applets – Applet Life cycle – creating an
Executable Applet – Designing a Web Page – Adding Applet to HTML File –
Passing Parameters to Applets.
Java
import java.applet.Applet;
import java.awt.Graphics;
This applet simply displays the message "Hello, world!" on the screen. To
embed this applet in an HTML page, you would use the following code:
1
HTML
<applet code="HelloWorldApplet.class" width="300" height="100"></applet>
When a user visits this web page, their browser will download the
HelloWorldApplet.class file and run the applet.
Applets were once very popular, but their use has declined in recent years. This
is due to a number of factors, including the rise of other web technologies,
such as Flash and JavaScript, and security concerns. However, applets are still
used in some applications, such as online calculators and educational games.
❖ Applets are Java programs that can be embedded in a web page and run
within a web browser. They are typically used to add interactivity or
dynamic content to web pages. Applets cannot access local resources on
the user's computer, and they have limited security privileges.
❖ Java applets are Java programs that are embedded in web pages and run
within a web browser. They cannot access local resources on the user's
computer, and they have limited security privileges.
File access Java application programs Applets don’t have local disk
have full access to the and network access.
local file system and
network.
Access level Applications can access Applets can only access browser
all kinds of resources specific services. They don’t
available on the system. have access to the local system.
Installation First and foremost, the The Java applet does not need to
installation of a Java be installed beforehand.
application on the local
computer is required.
Run It cannot run on its own; It cannot start on its own, but it
it needs JRE to execute. can be executed using a
Java-enabled web browser.
Read and It supports the reading It does not support the reading
Write and writing of files on and writing of files on the local
Operation the local computer. computer.
5
APPLET LIFE CYCLE:
The applet life cycle is the process by which an applet is created, initialized,
started, stopped, and destroyed. The applet life cycle is managed by the web
browser in which the applet is running.
The applet life cycle begins when the applet is loaded into the web browser.
The browser first calls the applet's init() method. The init() method is used to
initialize the applet and prepare it to run.
Once the applet has been initialized, the browser calls the applet's start()
method. The start() method is used to start the applet running. The start()
method is called each time the applet becomes visible, such as when the user
scrolls to the applet on a web page.
When the applet is no longer visible, or when the user navigates away from the
web page, the browser calls the applet's stop() method. The stop() method is
used to stop the applet from running.
Finally, when the applet is unloaded from the web page, the browser calls the
applet's destroy() method. The destroy() method is used to clean up any
resources that the applet is using.
6
The following table summarizes the different stages of the applet life cycle and
the methods that are called at each stage:
Stage Method called Description
init()
Initialized The applet is initialized and prepared to
start()
Started run. The applet is started running.
-
Running The applet is running and interacting with the
stop()
Stopped user. The applet is stopped from running.
destroy()
It is important to note that the applet life cycle is not strictly linear. For
example, if an applet is running and the user navigates to another web page,
the browser may call the applet's stop() method to stop the applet from
running. If the user then navigates back to the web page containing the applet,
the browser will call the applet's start() method to restart the applet.
Applets can also be started and stopped programmatically by calling the start()
and stop() methods directly. This is typically done by other Java programs that
are running in the same web browser.
The applet life cycle is an important concept to understand when developing
Java applets. By understanding the applet life cycle, you can develop applets
that are more efficient and reliable.
7
CREATING AN EXECUTABLE APPLET:
To create an executable applet, you need to use the Java compiler to compile your
applet code into a bytecode file. The bytecode file can then be packaged into a JAR
file, which is a Java archive file.
To compile your applet code, you can use the following command:
javac MyApplet.java
To package your applet into a JAR file, you can use the jar command-line tool. The
following command will create a JAR file called myapplet.jar containing your applet
code:
Once you have created a JAR file, you can distribute it to users by placing it on a
web server. Users can then run the applet by embedding it in a web page.To embed
an applet in a web page, you can use the <applet> tag. The <applet> tag has a
number of attributes that you can use to control the applet, such as its width and
height.
The following HTML code shows how to embed an applet in a web page:
HTML
The code attribute specifies the name of the applet class file. The width and
height attributes specify the width and height of the applet in pixels.Once you have
embedded the applet in your web page, you can run the applet by opening the web
page in a web browser.
Java
import java.applet.Applet;
import java.awt.Graphics;
8
To compile and run this applet, you would follow these steps:
1. Create a new file called MyApplet.java and paste the above code into it.
2. Compile the applet code using the following command:
javac MyApplet.java
This will create a JAR file called myapplet.jar containing your applet
code. 4. Create a new HTML file and paste the following code into it:
HTML
<!DOCTYPE html>
<html>
<head>
<title>My Applet</title>
</head>
<body>
<applet code="MyApplet.class" width="300"
height="200"></applet> </body>
</html>
∙ Purpose: What is the purpose of the web page? What do you want visitors to
do when they land on the page?
∙ Target audience: Who is the web page for? What are their needs and
interests?
∙ Content: What content will be included on the web page? How will it be
organized and presented?
∙ Design: What will the web page look like? What colors, fonts, and images will
be used?
9
∙ Navigation: How will users be able to navigate around the web page?
∙ User experience: How easy will it be for users to use the web page? Will they
be able to find the information they need quickly and easily?
Once you have considered all of these factors, you can start to design your web
page.
∙ Use a clear and concise layout. The layout of your web page should be easy to
understand and navigate. Use clear headings and subheadings to organize
your content.
∙ Use a consistent design. The design of your web page should be consistent
throughout. Use the same colors, fonts, and images on every page.
∙ Make sure your web page is mobile-friendly. More and more people are using
their smartphones and tablets to access the internet. Make sure your web
page looks good and functions well on all devices.
∙ Use high-quality images and videos. Images and videos can make your web
page more visually appealing and engaging. However, make sure to use high
quality images and videos that are relevant to your content.
∙ Test your web page thoroughly. Before you publish your web page, make sure
to test it thoroughly on different devices and browsers. This will help you to
identify and fix any problems.
Here are some additional tips for designing a web page that is effective and user
friendly:
∙ Use a minimalist design. Don't overload your web page with too much content
or too many design elements. A minimalist design will make your web page
easier to read and scannable.
∙ Use white space effectively. White space is the empty space around and
between the elements on your web page. It is important to use white space
effectively to make your web page look less cluttered and more inviting.
∙ Use clear and concise language. Avoid using jargon or technical terms that
your target audience may not understand. Use clear and concise language
that is easy to read and understand.
∙ Make your web page accessible. Make sure your web page is accessible to
all users, including those with disabilities. Use high-contrast colors, large
fonts, and alternative text for images.
By following these tips, you can design a web page that is effective, user-friendly,
and visually appealing.
10
Example:
<!DOCTYPE html>
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<h1>My Web Page</h1>
<p>This is my web page. Here you can learn more about me and my work.</p>
<ul>
<li><a href="about.html">About Me</a></li>
<li><a href="work.html">My Work</a></li>
</ul>
</body>
</html>
The following HTML code shows how to embed an applet in a web page:
HTML
<applet code="MyApplet.class" width="300" height="200"></applet>
The code attribute specifies the name of the applet class file. The width and
height attributes specify the width and height of the applet in pixels.
You can place the <applet> tag anywhere in the HTML file, but it is typically placed
in the <body> section of the file.
HTML
<!DOCTYPE html>
<html>
<head>
<title>My Applet</title>
</head>
11
<body>
</body>
</html>
To run this applet, you would save the HTML file as myapplet.html and then open it
in a web browser.You can also pass parameters to an applet by using the <param>
tag. The <param> tag has two attributes: name and value. The name attribute
specifies the name of the parameter, and the value attribute specifies the value of
the parameter.
HTML
<applet code="MyApplet.class" width="300" height="200">
<param name="myParameter" value="myValue">
</applet>
The myParameter parameter will be passed to the applet's constructor. The applet
can then access the parameter using the getParameter() method.
Applets are a powerful way to add interactivity and dynamic content to web pages.
However, it is important to note that applets are no longer as widely used as they
once were, due to security concerns and the rise of other technologies such as
JavaScript.
HTML
<applet code="MyApplet.class" width="300" height="200">
<param name="myParameter" value="myValue">
</applet>
12
For example, the following applet code shows how to retrieve and
display the value of the myParameter parameter:
Java
import java.applet.Applet;
import java.awt.Graphics;
To run this applet, you would save the HTML file as myapplet.html and then
open it in a web browser. You should see the value of the myParameter
parameter displayed in the applet.
HTML
<applet code="MyApplet.class" width="300" height="200">
<param name="myParameter1" value="myValue1">
<param name="myParameter2" value="myValue2">
</applet>
You can then access these parameters in the applet code using the
getParameter() method.