Introduction to Java Applets2
Introduction to Java Applets2
Advantages of Applets:
Disadvantages of Applets:
Applets:
o No main() method.
o Run inside a web browser or an applet viewer.
o Must extend Applet or JApplet.
Standalone Applications:
Java Applets have a defined lifecycle controlled by specific methods called by the
applet container:
init() Method:
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:
stop() Method:
o Called when the user navigates away from the applet’s page.
o Used to stop animations or media playback.
destroy() Method:
Running Applets:
Example:
Example:
appletviewer example.html
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.
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:
Applets can be used to create graphical user interfaces (GUIs) using AWT
and Swing components.
To use Swing components (JButton, JLabel):
Example:
import javax.swing.*;
public class AddButton extends JApplet {
public void init() {
add(new JButton("Click Me!"));
}
}
Displaying Images:
Example:
g.drawImage(img, 50, 50, this);
o
o
Example:
Example:
Methods available:
o play()
o loop()
o stop()
Security Considerations:
Answer: b
Which method is called first when an applet is initialized?
a) start()
b) init()
c) paint()
d) stop()
Answer: b
Answer: a
Answer: c
Answer: b
Answer: b
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
Answer: c
Answer: a
Answer: b
Answer: b
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
Answer: c
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.
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.
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.
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.
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.
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.
Applets run in a sandbox security model, which restricts their access to system resources
(like files and network connections) to prevent malicious activity.
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.
An image can be displayed using the drawImage() method from the Graphics
class. Example:
The getParameter() method is used to retrieve values passed from an HTML page
to the applet via the <param> tag.
1. Initialization (init())
2. Starting (start())
3. Painting (paint())
4. Stopping (stop())
5. Destroying (destroy())
The paint() method is used for rendering graphical content, such as drawings,
images, and text within the applet window.
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.
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)
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
Explain the difference between the Applet and JApplet classes. When
would you use each?
Answer:
Describe the security restrictions imposed on Java applets and how they
affect applet capabilities.
Answer:
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:
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.
1.
2.
3. Java Code Exampl
4.
5.
6.