0% found this document useful (0 votes)
5 views12 pages

Introduction to Java Applets2

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
5 views12 pages

Introduction to Java Applets2

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 12

Introduction to Java Applets:

 A Java Applet is a special type of Java program designed to be embedded


within web pages to create dynamic content.
 Applets are primarily used for Internet computing and can be embedded in
HTML documents using the <applet> tag.
 They run on the client-side within a web browser equipped with a Java
Virtual Machine (JVM).

How Applets Work:

 When a Java-enabled browser loads a web page containing an applet:

o The applet's code is downloaded to the client’s browser.


o The JVM within the browser executes the applet.

 Applets must extend the java.applet.Applet class to function properly.

Advantages of Applets:

 Platform Independence: Applets can run on any system with a Java-enabled


browser.
 Reduced Server Load: Applets execute on the client side, reducing the load
on the server.
 Scalability: Workload can be shifted from the server to the client.
 Real-Time Interaction: Applets provide faster response times since they
work on the client side.

Disadvantages of Applets:

 Plugin Requirement: A compatible Java plugin must be installed in the


browser.
 Security Restrictions: Applets run in a sandbox environment, restricting file
access and network connections for security reasons.
 Browser Compatibility: Many modern browsers no longer support Java
Applets due to security concerns.

Difference Between Applets and Standalone Applications:

 Applets:

o No main() method.
o Run inside a web browser or an applet viewer.
o Must extend Applet or JApplet.

 Standalone Applications:

o Require a main() method.


o Run independently as a desktop application.
 Applets are specifically designed for web-based graphical content while
standalone applications are general-purpose Java programs.

Applet Lifecycle Methods:

Java Applets have a defined lifecycle controlled by specific methods called by the
applet container:

init() Method:

o Called once when the applet is initialized.


o Used for setting up initial configurations like loading parameters.

start() Method:

o Called after init() and when the user returns to the applet's page.
o Used to start animations or media playback.

paint(Graphics g) Method:

o Called to render graphics on the applet.


o Utilizes the Graphics object to draw shapes, images, and text.

stop() Method:

o Called when the user navigates away from the applet’s page.
o Used to stop animations or media playback.

destroy() Method:

o Called when the applet is permanently removed.


o Used for resource cleanup.

Running Applets:

Java Applets can be executed using two methods:

1. HTML File with <applet> Tag:

o The <applet> tag embeds the applet in a webpage.


o Parameters can be passed using the <param> tag.

Example:

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


<param name="message" value="Hello Applet!">
</applet>

2. Using the appletviewer Tool:


o A testing utility included with the JDK.

Example:

appletviewer example.html

Creating HTML Files for Applets:

 The <applet> tag specifies the applet class file and dimensions.
 Attributes like codebase, width, height, and <param> tags can be used to
customize the applet behavior.

Passing Parameters to Applets:

 Parameters can be passed from the HTML file using the <param> tag.
 The getParameter(String name) method in the Applet class retrieves the
parameter values.

Example:

public void init() {


String message = getParameter("message");
System.out.println(message);
}

Applet and GUI Programming:

 Applets can be used to create graphical user interfaces (GUIs) using AWT
and Swing components.
 To use Swing components (JButton, JLabel):

o Use the javax.swing.JApplet class.

Example:

import javax.swing.*;
public class AddButton extends JApplet {
public void init() {
add(new JButton("Click Me!"));
}
}

Graphics, Images, and Animations in Applets:

 Displaying Images:

o Use the drawImage() method from the Graphics class.

Example:
g.drawImage(img, 50, 50, this);

o
o

 Animation with Threads:

o Loops and threads can be used to create basic animations.

Example:

for (int i = 0; i < 500; i++) {


g.drawImage(img, i, 50, this);
Thread.sleep(100);
}

Playing Audio in Applets:

 Java supports audio playback using the AudioClip class.

Example:

AudioClip audio = getAudioClip(getCodeBase(), "sound.wav");


audio.play();

 Methods available:

o play()
o loop()
o stop()

Security Considerations:

 Applets run in a sandbox with limited permissions:

o Restricted access to local files.


o Limited network access.

 This ensures security but can limit functionality.

Multiple Choice Questions (MCQs):

What is a Java Applet?


a) A standalone Java application
b) A small Java program embedded in a webpage
c) A Java program used for server-side scripting
d) A Java application running on a database

Answer: b
Which method is called first when an applet is initialized?
a) start()
b) init()
c) paint()
d) stop()

Answer: b

What is required to run a Java Applet in a web browser?


a) JVM (Java Virtual Machine)
b) Java SDK
c) JDK
d) Java Editor

Answer: a

Which class must all applets extend?


a) javax.swing.JFrame
b) java.awt.Graphics
c) java.applet.Applet
d) javax.swing.JApplet

Answer: c

What is the purpose of the paint() method in an applet?


a) To initialize applet parameters
b) To draw graphics on the screen
c) To destroy the applet
d) To start the applet

Answer: b

Which lifecycle method is used to stop an applet?


a) init()
b) stop()
c) destroy()
d) start()

Answer: b

What tag is used to embed a Java Applet in an HTML file?


a) <embed>
b) <java>
c) <applet>
d) <script>

Answer: c
Which tool can be used to test an applet without a web browser?
a) javac
b) java
c) appletviewer
d) jar

Answer: c

What is the main disadvantage of Java Applets?


a) Easy to use
b) Limited security restrictions
c) Requires a plugin for the browser
d) Platform independent

Answer: c

Which method is used to read parameters passed to an applet?


a) getParameter()
b) readParam()
c) fetchParam()
d) loadParam()

Answer: a

What is the default security model used by Java Applets?


a) Firewall security
b) Sandbox security
c) HTTP security
d) Kernel security

Answer: b

How can audio be played in a Java Applet?


a) playSound()
b) AudioClip class
c) SoundPlayer class
d) AudioManager class

Answer: b

Which of the following is NOT a valid lifecycle method?


a) destroy()
b) start()
c) execute()
d) stop()

Answer: c
What does the <param> tag do in an applet?
a) Specifies the applet size
b) Passes parameters to the applet
c) Loads the applet class
d) Embeds images in the applet

Answer: b

What method is used to display an image in an applet?


a) draw()
b) paint()
c) drawImage()
d) displayImage()

Answer: c

Answers to Short Answer Questions:

What is the primary purpose of a Java Applet?

The primary purpose of a Java Applet is to create dynamic content on web pages by
embedding small Java programs that run on the client-side within a browser.

Explain the difference between JApplet and Applet.

Applet belongs to the AWT (Abstract Window Toolkit) package, whereas JApplet
belongs to the Swing package, providing more advanced GUI components and better
support for modern graphical applications.

Name two advantages of using Java Applets.

Platform Independence: Applets can run on any device with a Java-enabled browser.

Reduced Server Load: Applets execute on the client-side, reducing server processing
demands.

What is the role of the init() method in an Applet?

The init() method is used for initializing the applet when it is first loaded. It is
commonly used to set up variables and fetch initial data such as HTML parameters.

What does the start() method do in the applet lifecycle?

The start() method is called after init() and resumes execution when the applet's
web page is revisited. It is commonly used to start animations or begin tasks.

How can you run an applet without a web browser?

An applet can be run without a web browser using the appletviewer tool provided
in the Java Development Kit (JDK).
What is the <applet> tag used for?

The <applet> tag in HTML is used to embed a Java Applet in a web page, specifying
the applet’s class file and dimensions.

Why are applets considered less secure?

Applets run in a sandbox security model, which restricts their access to system resources
(like files and network connections) to prevent malicious activity.

What is the significance of the stop() method?

The stop() method halts the execution of the applet when the user navigates away
from the applet’s web page or when it becomes inactive.

How can an image be displayed in a Java Applet?

An image can be displayed using the drawImage() method from the Graphics
class. Example:

g.drawImage(img, 50, 50, this);

What is the purpose of the getParameter() method?

The getParameter() method is used to retrieve values passed from an HTML page
to the applet via the <param> tag.

Explain the concept of a sandbox in Java Applets.

A sandbox is a restricted execution environment where applets run, preventing access


to system resources for security reasons (e.g., no access to local files or unauthorized
network communication).

What are the five states in an applet's lifecycle?

The five states in an applet’s lifecycle are:

1. Initialization (init())
2. Starting (start())
3. Painting (paint())
4. Stopping (stop())
5. Destroying (destroy())

Write an example of an HTML tag to embed an applet.

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


<param name="message" value="Hello, Applet!">
</applet>

What is the difference between paint() and init() methods in an applet?


The init() method initializes the applet when it loads and sets up initial
configurations.

The paint() method is used for rendering graphical content, such as drawings,
images, and text within the applet window.

Short Answer Questions

Explain the purpose and working of the paint() method in a Java applet.
Answer: The paint() method in Java applets is used to perform all the
graphical tasks, such as drawing shapes, text, or images on the applet window.
It is automatically called after start() and whenever the applet needs to be
repainted. It receives a Graphics object as a parameter, which provides the
methods required for drawing on the screen.

What are the advantages of using Java applets?


Answer:

1. Applets can run on multiple platforms with a compatible Java-enabled browser.


2. They can reduce server load by performing operations on the client side.
3. They offer dynamic content generation for web pages.
4. Applets support multimedia like audio, images, and animations.

List and describe the major differences between an applet and a standard
Java GUI application.
Answer:

1. Execution: An applet is executed within a web browser, while a standard Java GUI
application runs independently.
2. Entry Point: Applets do not have a main() method, while standard applications
do.
3. Security: Applets run in a restricted environment (sandbox) for security reasons,
whereas standalone applications can access system resources more freely.
4. Use Case: Applets are designed for web-based interaction, while standard
applications are for general desktop use.

Describe the security restrictions applied to Java applets and why they
exist.
Answer: Java applets operate under a sandbox security model, which
restricts them from performing potentially harmful operations such as
accessing local files, modifying system settings, or executing external
programs. These restrictions exist to prevent unauthorized code execution and
protect user data when running untrusted applets from the web.

How does an applet interact with an HTML page, and how can it receive
input from the user?
Answer: An applet interacts with an HTML page using the <applet> tag. It
can receive input through parameters defined using the <param> tag within
the applet tag. The applet can access these parameters using the
getParameter() method inside the init() method.
Multiple Choice Questions (MCQs)

Which method in the Applet class is used to retrieve an image from a


URL?
a) loadImage()
b) fetchImage()
c) getImage()
d) drawImage()

What happens when the stop() method is called in a Java applet?


a) The applet is terminated immediately.
b) The applet's execution pauses until start() is called again.
c) The applet's thread is destroyed permanently.
d) The applet is restarted automatically

Which of the following correctly describes the difference between the


init() and start() methods?
a) init() is called multiple times, while start() is called only once.
b) start() initializes resources, while init() is used for event handling.
c) init() is used for initialization and called once, while start() is called
every time the applet is visited.
d) Both methods are identical and interchangeable.

What is the primary purpose of the paint() method in a Java applet?


a) To start the applet
b) To handle GUI interactions
c) To draw graphics and update the applet’s content
d) To stop the applet’s execution

Which interface must be implemented for an applet to respond to mouse


events?
a) MouseInputListener
b) MouseListener
c) MouseAdapter
d) MouseEvent

True/False Questions (Advanced)

1. True/False: The appletviewer tool can be used to test applets without an HTML file.
2. True/False: Java applets can access the local file system without restrictions when run in a
web browser.
3. True/False: The JApplet class extends the Applet class and can work with both AWT and
Swing components.
4. True/False: The stop() method in an applet is called before the destroy() method.
5. True/False: An applet can play audio files using the AudioClip class.

· True: The appletviewer tool can test applets directly using an HTML file without a web
browser.
· False: Java applets cannot access the local file system due to sandbox restrictions.
· True: JApplet extends Applet and supports both AWT and Swing components.
· True: The stop() method is called before destroy() during the applet's life cycle.
· True: The AudioClip class can be used to play audio in Java applets

Fill in the Blanks (Advanced)

1. The __________ method is responsible for rendering graphics in an applet.


2. The __________ method is called when the applet is loaded but before it becomes visible.
3. Applets cannot access local system resources directly due to the __________ security
model.
4. To run an applet without a web browser, the __________ tool can be used.
5. The method used to play an audio file in an applet is __________.

Short Answer Questions (Advanced)

Explain the difference between the Applet and JApplet classes. When
would you use each?
Answer:

1. Applet: Part of the AWT package, supports basic GUI elements.


2. JApplet: Part of the Swing library, supports richer GUI components like JButton
and JLabel.
3. Use Case: JApplet is preferred when working with Swing-based applications
requiring complex GUI components.

Describe the security restrictions imposed on Java applets and how they
affect applet capabilities.
Answer:

1. Java applets run in a sandbox security model, restricting:

1. File system access


2. Network connections (limited to the server it originated from)
3. Executing system commands

2. This prevents malicious code from harming the user's system but also limits
advanced functionalities.

2.

How does an applet's life cycle differ from a standard Java application's
life cycle?
Answer:

1. Applet Life Cycle: init(), start(), paint(), stop(), destroy()


2. Java Application Life Cycle: Begins with main() and ends when the program exits.
3. Applets have dedicated life cycle methods for handling loading, rendering, and
cleanup, while applications rely on a main() method as the entry point.

What is the purpose of the paint(Graphics g) method in an applet? How


can double buffering be used to avoid flickering in an applet?
Answer:

1. Purpose: The paint(Graphics g) method handles drawing elements like


shapes, text, and images. It is called automatically when the applet needs to be
redrawn.
2. Double Buffering:

1. Double buffering reduces flickering by rendering graphics in an off-screen


buffer before displaying them.
2. Steps: Draw content on a hidden buffer → Transfer it to the visible screen.

Explain the use of the <param> tag in HTML and how it can be used to
pass data to an applet. Provide an example.
Answer:

The <param> tag allows passing parameters from an HTML file to an applet.

<applet code="MyApplet.class" width="300"


height="200">
<param name="greeting" value="Hello
Applet!"></applet>

1.
2.
3. Java Code Exampl
4.

public void init() {


String greeting = getParameter("greeting");
System.out.println(greeting);
}

5.
6.

You might also like