Chapter 2
Chapter 2
JAVA APPLET
Java Applets are small Java programs that run within a web browser, typically enhancing web
pages with interactive content. They provide a way to create dynamic and engaging user
interfaces directly in the browser, bringing a level of interactivity beyond what static HTML
pages offer. Java applets are small, self-contained programs written in the Java programming
language that are designed to be embedded within HTML pages. They run on the client-side,
providing dynamic and interactive content on web browsers. Java applets are executed within a
Java Virtual Machine (JVM), enabling platform independence and security features.
Brief History: Java applets were introduced by Sun Microsystems in the mid-1990s as a
revolutionary approach to web development. At that time, the web was primarily static, and Java
applets allowed developers to bring dynamic, interactive, and multimedia-rich content to web
pages. The "Write Once, Run Anywhere" (WORA) principle of Java, enabled by the JVM,
played a significant role in the initial popularity of Java applets.
Interactive Web Content: Applets were initially created to bring interactivity and
dynamic content to web browsers.
Platform Independence: Java's "Write Once, Run Anywhere" principle allowed applets
to run on various platforms with a JVM.
1. Initialization (init() method): The init() method is the first method called when an
applet is loaded. It is used for one-time initialization tasks, such as setting up graphical
components, initializing variables, or loading resources. The init () method is called only
once during the applet's lifetime.
3 2. Starting (start() method): The start() method is called after the init() method and
whenever the user revisits a page containing the applet or interacts with the applet in
a. AppletViewer
b. Web browsers.
1. Applet Viewer: The AppletViewer is a standalone command-line tool provided by the Java
Development Kit (JDK). It is used for testing and running Java applets without the need for a
web browser.
Usage
Developers can use the AppletViewer during the development and testing
4
phases.
Simplifies the testing process, enabling developers to identify and fix issues in the
applet's logic or user interface.
Disadvantages:
2. Web Browser: Java applets are primarily designed to be executed within web browsers.
They are embedded into HTML documents and run on the client side, enhancing the
interactivity and visual appeal of web pages.
Usage:
Applets are typically deployed on web servers and accessed by users through web
pages.
Web browsers such as Chrome, Firefox, Safari, and Internet Explorer support the
execution of Java applets.
Advantages:
Enables the integration of dynamic and interactive content seamlessly into web
pages.
Disadvantages:
5
Security restrictions imposed by modern browsers have limited the use of Java
applets.
Code: Specifies the name of the applet's main class file without the .class extension.
Example: <applet code="MyApplet" width="300" height="200"></applet>
Width: Specifies the width of the applet in pixels.
Example: <applet code="MyApplet" width="300" height="200"></applet>
Height: Specifies the height of the applet in pixels.
Example: `<applet code="MyApplet" width="300" height="200"></applet>`
alt: Specifies alternative text to be displayed if the browser does not support Java or if the
applet fails to load.
Example:<applet code="MyApplet" width="300" height="200"
alt="Java applet is not supported."></applet>
Name: Provides a name for the applet, which can be used for referencing the applet in
JavaScript or within the HTML document.
Example: <applet code="MyApplet" width="300" height="200"
name="myApplet"></applet>
Archive: Specifies a comma-separated list of archive files containing classes and
resources required by the applet.
Example: <applet code="MyApplet" width="300" height="200"
archive="myApplet.jar"></applet>
Align: Specifies the alignment of the applet within the surrounding HTML content (e.g.,
"left," "right," "center").
Example: <applet code="MyApplet" width="300" height="200"
align="center"></applet>
hspace, vspace: Specifies horizontal and vertical space around the applet, respectively.
</applet>
import java.applet.Applet;
import java.awt.Graphics;
public class xyz extends Applet {
public void paint(Graphics g) {
g.drawString("Hello, World!", 20, 20);}}
Example: xxy.html
<!DOCTYPE html>
<html>
<head>
<title> This is my first program for java applet </title>
</head>
<body>
<applet code="xyz.class" width="300" height="100">
</applet>
</body>
7 </html>
This simple Java Applet displays the text "Hello, World!" on the applet canvas. The paint
method is a standard method in applets used to draw graphics.
Graphics in Applets
Graphics in Java applets involve the use of the java.awt.Graphics class to draw various shapes,
text, and images on the applet's surface. Here's a brief explanation of the main components
involved in graphics programming within Java applets:
1. java.awt.Graphics Class:
The Graphics class is a part of the Abstract Window Toolkit (AWT) package in Java.
It provides methods for drawing on components such as applets, panels, and frames.
Graphics operations are performed on an instance of the Graphics class, which
represents the drawing context.
2. Drawing Shapes:
Lines (drawLine): Draws a line between two points.
g.drawLine(x1, y1, x2, y2);
Rectangles (drawRect, fillRect): Draws or fills a rectangle.
g.drawRect(x, y, width, height);
g.fillRect(x, y, width, height);
Ovals (drawOval, fillOval): Draws or fills an oval.
3. Drawing Text:
g.drawImage(img, x, y, this);
g.setColor(Color.RED);
g.setFont(font);
Example Code:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
// Drawing shapes
g.setColor(Color.BLUE);
// Filling shapes
g.setColor(Color.GREEN);
// Drawing text
g.setColor(Color.BLACK);
g.setFont(font);
This applet draws rectangles, ovals, lines, and text, showcasing the basic operations you can
perform using the Graphics class in Java applet graphics programming.
Applet communication
Applet communication involves the exchange of information between Java applets, between
applets and HTML pages, or between applets and the server. There are several mechanisms for
applet communication, and here's a brief explanation of each:
11 same context (such as within the same HTML page) to communicate. Applets
can use `getApplet()` method to obtain a reference to another applet and call
its methods.
While both Java Applets and Java Applications are types of Java programs, they differ in their
purpose, deployment, and environment.
Java Applets:
12
Deployment: Applets are designed to be embedded within HTML pages and run in web
browsers. They are loaded and executed on the client-side.
Java Applications:
Deployment: Java Applications are standalone programs that are typically run on the
client's machine or a server. They are not designed to be embedded in web pages.
User Interface: Applications can have both command-line interfaces (CLI) and
graphical user interfaces (GUI) using AWT, Swing, or JavaFX.
Security: Applications have more flexibility and can access local resources with fewer
restrictions compared to applets. However, they still follow Java's security model.
Lifecycle Methods: Java Applications do not have specific lifecycle methods like
applets. They start executing from the main method.
14