0% found this document useful (0 votes)
10 views14 pages

Applet Programming: Unit:5

Uploaded by

Bhuvaneshwari M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views14 pages

Applet Programming: Unit:5

Uploaded by

Bhuvaneshwari M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

APPLET PROGRAMMING

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.

WHAT IS APPLET PROGRAMMING:


Applet programming is the creation of small Java programs that can be
embedded in HTML pages and run in web browsers. Applets are downloaded to
the user's computer when they visit a web page that contains an applet, and
then run inside the browser. Applets have many advantages over other types of
web content, such as images and videos. They can be interactive, allowing users
to do things like play games, solve puzzles, and perform calculations. They can
also be dynamic, changing their appearance and behavior in response to user
input. Applets are created by extending the java.applet.Applet class. This class
provides a number of methods that can be used to initialize, start, stop, and
destroy the applet. Applets also have access to the full range of Java APIs, so
they can be used to perform a wide variety of tasks.

Here is a simple example of an applet:

Java
import java.applet.Applet;
import java.awt.Graphics;

public class HelloWorldApplet extends Applet {

public void paint(Graphics g) {


g.drawString("Hello, world!", 10, 20);
}
}

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.

Advantages of applet programming


⮚ Interactive: Applets can be interactive, allowing users to do things like
play games, solve puzzles, and perform calculations.
⮚ Dynamic: Applets can be dynamic, changing their appearance and
behavior in response to user input.
⮚ Platform-independent: Java applets can run on any platform that has a
Java Virtual Machine (JVM).
⮚ Secure: Applets run in a sandboxed environment, which helps to protect
the user's computer from malicious code.

Disadvantages of applet programming


⮚ Slow performance: Applets can be slower than other types of web
content, such as images and videos.
⮚ Security concerns: Applets can be a security risk if they are not properly
programmed.
⮚ Declining popularity: The use of applets has declined in recent years due
to the rise of other web technologies, such as Flash and JavaScript. ⮚
Overall, applet programming is a powerful tool for creating interactive and
dynamic web content. However, it is important to be aware of the
disadvantages of applets, such as slow performance and security concerns.
2
Difference between Applications and Applets
❖ Applications are standalone programs that can be run without a web
browser. They are typically installed on the user's computer and can
access local resources, such as the file system and network.

❖ 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.

The difference between Java applications and applets is in their


execution environment and capabilities.
❖ Java applications are standalone programs that run on the user's
computer without the need for a web browser. They can access local
resources such as the file system and network, and they have full
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.

Examples of Java applications:


❖ A desktop word processor
❖ A database management system
❖ A video game
❖ A server application
Examples of Java applets:
❖ A calculator applet
❖ A game applet
❖ A stock ticker applet
❖ A chat applet
3

Parameters Java Application Java Applet

Definition Applications are just like Applets are small Java


a Java program that can programs that are designed to
be executed be included with the HTML
independently web document.
without using the web They require a Java-enabled
browser. web browser for execution.

main () The application program The applet does not require


method requires a main() method the main() method for its
for its execution. execution instead init()
method is required.

Compilation The “javac” command is Applet programs are compiled


used to compile with the “javac” command and
application programs, run using either the
which are then executed “appletviewer” command or the
using the “java” web browser.
command.

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.

Parameters Java Application Java Applet

Execution Applications can execute Applets cannot execute


the programs from the programs from the local
local system. machine.

Program An application program An applet program is needed


is needed to perform to perform small tasks or part
some tasks directly for of them.
the user.

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.

Connection Connectivity with other It is unable to connect to


with servers servers is possible. other servers.

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.

Security Application can access the Executed in a more restricted


system’s data and environment with tighter security.
resources without any They can only use services that
security are exclusive to their browser.
limitations.
Restrictions Java applications are self Applet programs cannot run
contained and require no on their own, necessitating
additional security the
because they are trusted. maximum level of security.

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 following diagram shows the applet life cycle:

[Initialized] -> [Started] -> [Running] -> [Stopped] -> [Destroyed]

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

Destroyed The applet is unloaded from the web page and


any resources it is using are cleaned up.

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

This will create a bytecode file called MyApplet.class.

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:

jar cvf myapplet.jar MyApplet.class

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

<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.Once you have
embedded the applet in your web page, you can run the applet by opening the web
page in a web browser.

Here is an example of a simple applet:

Java
import java.applet.Applet;
import java.awt.Graphics;

public class MyApplet extends Applet {


public void paint(Graphics g) {
g.drawString("Hello, world!", 100, 100);
}
}

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 bytecode file called MyApplet.class.


3. Package the applet into a JAR file using the following command:
jar cvf myapplet.jar MyApplet.class

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>

5. Open the HTML file in a web browser to run the applet.

DESIGNING A WEB PAGE:


Designing a web page is the process of planning, conceptualizing, and arranging
content online. It involves considering the overall layout, navigation, visual design,
and user experience of the page.

When designing a web page, it is important to keep the following in mind:

∙ 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.

Here are some tips for designing a 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>

ADDING APPLET TO HTML FILE:


To add an applet to an HTML file, 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
<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.

Here is an example of a complete HTML file containing an applet:

HTML
<!DOCTYPE html>
<html>
<head>
<title>My Applet</title>
</head>

11
<body>

<applet code="MyApplet.class" width="300" height="200"></applet>

</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.

The following HTML code shows how to pass a parameter to an applet:

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.

PASSING PARAMETERS TO APPLETS:


To pass parameters to an applet, you can use 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.

The following HTML code shows how to pass a parameter to an


applet:

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.

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;

public class MyApplet extends Applet {


public void paint(Graphics g) {
String parameterValue = getParameter("myParameter");
g.drawString(parameterValue, 100, 100);
}
}

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.

You can pass multiple parameters to an applet by using multiple <param>


tags. For example, the following HTML code shows how to pass two
parameters to an 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.

Passing parameters to applets is a powerful way to customize the behavior


of applets and to make them more interactive.
13

You might also like